Difference between revisions of "PERL/C3/Exception-and-error-handling-in-PERL/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| Border = 1 | <center>'''Time'''</center> | <center>'''Narration'''</center> |- | 00:01 | Welcome to the''' Spoken Tutorial''' on''' Exception and error handling in PERL...")
 
Line 6: Line 6:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Welcome to the''' Spoken Tutorial''' on''' Exception and error handling in PERL'''
+
| Welcome to the''' Spoken Tutorial''' on''' Exception and error handling''' in '''PERL'''.
  
 
|-
 
|-
 
| 00:06
 
| 00:06
| In this tutorial we will learn to
+
| In this tutorial, we will learn to:
+
* '''Catch''' errors and
* Catch errors and
+
* Handle '''exception'''s.
* Handle exceptions
+
 
|-
 
|-
 
| 00:12
 
| 00:12
| For this tutorial, I am using
+
| For this tutorial, I am using:
 
+
 
* '''Ubuntu Linux 12.04''' operating system
 
* '''Ubuntu Linux 12.04''' operating system
* '''Perl 5.14.2'''
+
* '''Perl 5.14.2''' and the
* and the''' gedit''' Text Editor
+
* ''' gedit''' Text Editor.
  
 
|-
 
|-
 
| 00:23
 
| 00:23
|You can use any text editor of your choice.
+
|You can use any '''text editor''' of your choice.
  
 
|-
 
|-
 
| 00:27
 
| 00:27
| To follow this tutorial, you should have working knowledge of''' Perl''' Programming.
+
| To follow this tutorial, you should have working knowledge of''' Perl''' programming.
  
 
|-
 
|-
Line 36: Line 34:
 
|-
 
|-
 
|00:39
 
|00:39
| When an error occurs'''Exception handling''' deviates the execution of a program from the normal execution path.  
+
| When an '''error''' occurs, '''Exception handling''' deviates the execution of a program from the normal execution path.  
  
 
|-
 
|-
 
| 00:47
 
| 00:47
| '''Error handling''' helps to recover the program, without terminating the application.
+
| '''Error handling''' helps to recover the program, without terminating the '''application'''.
  
 
|-
 
|-
Line 51: Line 49:
 
|-
 
|-
 
| 01:07
 
| 01:07
| The''' die''' function immediately terminates the execution and displays the error message.
+
| The''' die''' function immediately terminates the execution and displays the '''error message'''.
  
 
|-
 
|-
 
| 01:13
 
| 01:13
| Let us understand the''' die''' function using a sample program, which I have already saved.
+
| Let us understand the''' die''' function using a sample program which I have already saved.
  
 
|-
 
|-
 
| 01:20
 
| 01:20
| Go to the''' terminal''' and type '''gedit die dot pl ampersand '''and press''' Enter'''
+
| Go to the''' terminal''' and type: '''gedit die dot pl ampersand '''and press''' Enter'''.
  
 
|-
 
|-
 
|01:29
 
|01:29
| This is code in''' die.pl''' file. Let us understand the code now.
+
| This is the code in 'die.pl' file. Let us understand the code now.
  
 
|-
 
|-
 
|01:35
 
|01:35
| Here we have defined a '''function divide''' which takes two '''input parameters'''
+
| Here, we have defined a function '''divide''' which takes two '''input parameters'''
 
+
i.e '''dollar numerator '''and '''dollar denominator'''.
i.e '''dollar numerator '''and''' dollar''' '''denominator'''
+
  
 
|-
 
|-
 
| 01:46
 
| 01:46
|'''At the rate underscore '''(@_) is a '''special variable '''used to pass the '''parameter list''' to the '''function'''.
+
|'''At the rate underscore '''(@_) is a '''special variable '''used to pass the '''parameter list''' to the function.
  
 
|-
 
|-
Line 81: Line 78:
 
|-
 
|-
 
| 01:57
 
| 01:57
|It will also display the error message for the user to read.
+
|It will also display the '''error message''' for the user to read. Else, it will print the output.
 
+
Else it will print the output.
+
 
+
  
 
|-
 
|-
 
| 02:05
 
| 02:05
| These are the '''function call statements'''.
+
| These are the '''function call''' statements.
  
 
|-
 
|-
 
| 02:08
 
| 02:08
|The first two times, the '''function''' is executed because the '''second parameter''' is not zero.
+
|The first two times, the function is executed because the second parameter is not zero.
  
 
|-
 
|-
 
| 02:15
 
| 02:15
| The third time, the '''denominator''' value is zero, so the '''die function''' is executed.
+
| The third time, the '''denominator''' value is zero, so the '''die''' function is executed.
  
 
|-
 
|-
 
| 02:23
 
| 02:23
  
| The last '''divide function '''will not be executed as the '''die function '''quits the script.
+
| The last '''divide''' function will not be executed as the '''die''' function quits the script.
  
 
|-
 
|-
 
| 02:29
 
| 02:29
| Press''' Ctrl + S''' to save the program.
+
| Press''' Ctrl + S''' to '''save''' the program.
  
 
|-
 
|-
 
| 02:32
 
| 02:32
| Let us execute the program.
+
| Let us '''execute''' the program.
  
 
|-
 
|-
 
| 02:35
 
| 02:35
| Switch back to the''' terminal''' and type, '''perl die dot pl''' and press''' Enter'''.
+
| Switch back to the terminal and type: '''perl die dot pl''' and press''' Enter'''.
  
 
|-
 
|-
 
|02:43
 
|02:43
 
| The output is displayed as shown here.
 
| The output is displayed as shown here.
 
+
"Can't divide by zero!
'''Can't divide by zero! -'''
+
  
 
|-
 
|-
Line 127: Line 120:
 
|-
 
|-
 
| 02:54
 
| 02:54
| Next, we will see how to use''' eval function '''in error handling.
+
| Next, we will see how to use''' eval''' function in error handling.
  
 
|-
 
|-
 
| 03:00
 
| 03:00
| '''eval function '''is used for handling run-time errors or exceptions.
+
| '''eval''' function is used for handling '''run-time error'''s or '''exception'''s.
  
 
|-
 
|-
 
| 03:06
 
| 03:06
|For example, built-in errors such as '''out of memory, divide by zero''' or user defined errors.
+
|For example, '''built-in error'''s such as '''out of memory, divide by zero''' or user defined errors.
  
 
|-
 
|-
 
| 03:14
 
| 03:14
| The general syntax for''' eval function''' is shown here.
+
| The general syntax for''' eval''' function is shown here.
  
 
|-
 
|-
 
| 03:19
 
| 03:19
|The''' dollar exclamation'''('''$!) '''special variable holds the error message, if any.
+
|The''' dollar exclamation'''($!) '''special variable''' holds the error message, if any.
  
 
|-
 
|-
 
| 03:25
 
| 03:25
|Otherwise, '''dollar exclamation( $!) '''holds an empty string.  That means it is evaluated as '''false'''.
+
|Otherwise, '''dollar exclamation( $!) '''holds an empty '''string'''.  That means it is evaluated as '''false'''.
  
 
|-
 
|-
Line 155: Line 148:
 
|-
 
|-
 
| 03:40
 
| 03:40
| Type '''gedit eval dot pl ampersand '''and press''' Enter'''
+
| Type: '''gedit eval dot pl ampersand '''and press''' Enter'''.
  
 
|-
 
|-
Line 164: Line 157:
 
|-
 
|-
 
| 03:54
 
| 03:54
| Here in our example,'''open FILE''' invokes the''' die statement, '''if it has trouble in opening a file''' “test.dat”'''
+
| Here, in our example,'''open FILE''' invokes the''' die''' statement, if it has trouble in opening a file “test.dat”.
  
 
|-
 
|-
 
| 04:05
 
| 04:05
| '''Perl''' gives the '''system error message''' from the last '''eval''' block to the variable '''dollar exclamation( $!)'''
+
| '''Perl''' gives the '''system error message''' from the last '''eval''' block to the variable '''dollar exclamation( $!)'''.
  
 
|-
 
|-
 
| 04:13
 
| 04:13
| Press '''Ctrl + S''' to save the file.
+
| Press '''Ctrl + S''' to '''save''' the file.
  
 
|-
 
|-
 
| 04:17
 
| 04:17
| Switch back to the''' terminal''' and type, '''perl eval dot pl''' and press''' Enter'''.
+
| Switch back to the terminal and type: '''perl eval dot pl''' and press''' Enter'''.
  
 
|-
 
|-
Line 184: Line 177:
 
|-
 
|-
 
| 04:30
 
| 04:30
| Let us see another example. This time we will see an error message returned from '''eval''' function using '''$@''' '''(dollar at the rate).'''
+
| Let us see another example. This time we will see an error message returned from '''eval''' function using '$@' ('''dollar at the rate''').
  
 
|-
 
|-
Line 196: Line 189:
 
|-
 
|-
 
| 04:48
 
| 04:48
|We are passing''' $total''', '''$count''' as input parameters to the '''function average'''.
+
|We are passing''' $total''', '''$count''' as input parameters to the function '''average'''.
  
 
|-
 
|-
 
| 04:56
 
| 04:56
|We have a possibility of getting an error if the count is zero.
+
|We have a possibility of getting an '''error''' if the '''count''' is zero.
  
 
|-
 
|-
 
| 05:00
 
| 05:00
|Here, that is handled with the '''die statement'''.
+
|Here, that is handled with the '''die''' statement.
  
 
|-
 
|-
 
| 05:04
 
| 05:04
|The error message returned from '''eval''' is displayed using '''$@ ( dollar at the rate)'''
+
|The error message returned from '''eval''' is displayed using '''$@ ( dollar at the rate)'''.
  
 
|-
 
|-
Line 216: Line 209:
 
|-
 
|-
 
| 05:15
 
| 05:15
| Press Ctrl +S to save the file.  Let us execute the program.
+
| Press '''Ctrl +S''' to '''save''' the file.  Let us '''execute''' the program.
  
 
|-
 
|-
 
| 05:22
 
| 05:22
| Switch back to the''' terminal''' and type, '''perl eval.pl''' and press''' Enter'''.
+
| Switch back to the terminal and type: '''perl eval.pl''' and press''' Enter'''.
  
 
|-
 
|-
Line 228: Line 221:
 
|-
 
|-
 
| 05:35
 
| 05:35
| This brings us to the end of this tutorial. Let us summarise.
+
| This brings us to the end of this tutorial. Let us summarize.
  
 
|-
 
|-
 
| 05:41
 
| 05:41
|In this tutorial, we have learnt how to
+
|In this tutorial, we have learnt how to:
 
+
 
* Catch errors and
 
* Catch errors and
* Handle exceptions
+
* Handle exceptions.
 
|-
 
|-
 
| 05:47
 
| 05:47
 
| As an assignment do the following.
 
| As an assignment do the following.
 
+
On your '''Linux''' machine, create a file 'emp.txt' with 5 '''employee''' names.  
On your '''Linux''' machine, create a file '''emp.txt''' with 5 employee names.  
+
  
 
|-
 
|-
 
| 05:57
 
| 05:57
|Change permission of '''emp.txt '''to '''READ''' only.
+
|Change permission of 'emp.txt' to '''READ''' only.
  
 
|-
 
|-
 
| 06:02
 
| 06:02
|'''Note''': Go through the relevant''' Linux''' spoken tutorials on the''' spoken tutorial''' website for '''change permission''' option.
+
|Note: Go through the relevant''' Linux''' spoken tutorials on the''' spoken tutorial''' website for '''change permission''' option.
  
 
|-
 
|-
 
| 06:10
 
| 06:10
|Write a '''Perl''' program to open the '''emp.txt''' file in '''WRITE''' mode and add few employee names to it.
+
|Write a '''Perl''' program to open the 'emp.txt' file in '''WRITE''' mode and add few employee names to it.
  
 
|-
 
|-
 
| 06:19
 
| 06:19
| Using "'''eval'''", print appropriate error message if '''open/write''' operation fails.
+
| Using "eval", print appropriate error message if '''open/write''' operation fails.
  
 
|-
 
|-
 
|06:26
 
|06:26
| The video at the following link summarises the Spoken Tutorial project.
+
| The video at the following link summarizes the '''Spoken Tutorial''' project. Please download and watch it.
Please download and watch it.
+
  
 
|-
 
|-
 
| 06:33
 
| 06:33
| The''' Spoken Tutorial Project''' Team
+
| The''' Spoken Tutorial Project''' team:
 
+
 
* conducts workshops using spoken tutorials and
 
* conducts workshops using spoken tutorials and
 
* gives certificates on passing online tests.
 
* gives certificates on passing online tests.
Line 283: Line 272:
 
|-
 
|-
 
| 06:58
 
| 06:58
| This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.
+
| This is Nirmala Venkat from '''IIT Bombay''', signing off. Thanks for watching.
  
 
|}
 
|}

Revision as of 22:21, 28 December 2015

Time
Narration
00:01 Welcome to the Spoken Tutorial on Exception and error handling in PERL.
00:06 In this tutorial, we will learn to:
  • Catch errors and
  • Handle exceptions.
00:12 For this tutorial, I am using:
  • Ubuntu Linux 12.04 operating system
  • Perl 5.14.2 and the
  • gedit Text Editor.
00:23 You can use any text editor of your choice.
00:27 To follow this tutorial, you should have working knowledge of Perl programming.
00:32 If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website.
00:39 When an error occurs, Exception handling deviates the execution of a program from the normal execution path.
00:47 Error handling helps to recover the program, without terminating the application.
00:53 We can identify and trap an error in a number of ways. We will see few commonly used methods in Perl.
01:01 The warn function only raises a warning message without taking further action.
01:07 The die function immediately terminates the execution and displays the error message.
01:13 Let us understand the die function using a sample program which I have already saved.
01:20 Go to the terminal and type: gedit die dot pl ampersand and press Enter.
01:29 This is the code in 'die.pl' file. Let us understand the code now.
01:35 Here, we have defined a function divide which takes two input parameters

i.e dollar numerator and dollar denominator.

01:46 At the rate underscore (@_) is a special variable used to pass the parameter list to the function.
01:53 If the denominator is zero, the die function will quit the script.
01:57 It will also display the error message for the user to read. Else, it will print the output.
02:05 These are the function call statements.
02:08 The first two times, the function is executed because the second parameter is not zero.
02:15 The third time, the denominator value is zero, so the die function is executed.
02:23 The last divide function will not be executed as the die function quits the script.
02:29 Press Ctrl + S to save the program.
02:32 Let us execute the program.
02:35 Switch back to the terminal and type: perl die dot pl and press Enter.
02:43 The output is displayed as shown here.

"Can't divide by zero!"

02:49 This is the error message we have given in the program in the die statement.
02:54 Next, we will see how to use eval function in error handling.
03:00 eval function is used for handling run-time errors or exceptions.
03:06 For example, built-in errors such as out of memory, divide by zero or user defined errors.
03:14 The general syntax for eval function is shown here.
03:19 The dollar exclamation($!) special variable holds the error message, if any.
03:25 Otherwise, dollar exclamation( $!) holds an empty string. That means it is evaluated as false.
03:33 Let us understand the eval function using a sample program.

Go to the terminal.

03:40 Type: gedit eval dot pl ampersand and press Enter.
03:47 In the eval dot pl file, type the following code as displayed on the screen.

Let me explain the code now.

03:54 Here, in our example,open FILE invokes the die statement, if it has trouble in opening a file “test.dat”.
04:05 Perl gives the system error message from the last eval block to the variable dollar exclamation( $!).
04:13 Press Ctrl + S to save the file.
04:17 Switch back to the terminal and type: perl eval dot pl and press Enter.
04:25 The system error message is displayed as shown here.
04:30 Let us see another example. This time we will see an error message returned from eval function using '$@' (dollar at the rate).
04:40 Let us switch back to the eval dot pl file.
04:44 Type the code as shown on the screen.
04:48 We are passing $total, $count as input parameters to the function average.
04:56 We have a possibility of getting an error if the count is zero.
05:00 Here, that is handled with the die statement.
05:04 The error message returned from eval is displayed using $@ ( dollar at the rate).
05:11 If not, it will print the Average value.
05:15 Press Ctrl +S to save the file. Let us execute the program.
05:22 Switch back to the terminal and type: perl eval.pl and press Enter.
05:31 The output is as shown here.
05:35 This brings us to the end of this tutorial. Let us summarize.
05:41 In this tutorial, we have learnt how to:
  • Catch errors and
  • Handle exceptions.
05:47 As an assignment do the following.

On your Linux machine, create a file 'emp.txt' with 5 employee names.

05:57 Change permission of 'emp.txt' to READ only.
06:02 Note: Go through the relevant Linux spoken tutorials on the spoken tutorial website for change permission option.
06:10 Write a Perl program to open the 'emp.txt' file in WRITE mode and add few employee names to it.
06:19 Using "eval", print appropriate error message if open/write operation fails.
06:26 The video at the following link summarizes the Spoken Tutorial project. Please download and watch it.
06:33 The Spoken Tutorial Project team:
  • conducts workshops using spoken tutorials and
  • gives certificates on passing online tests.
06:42 For more details, please write to us.
06:46 Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
06:53 More information on this mission is available at this link.
06:58 This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14