1 00:00:01 --> 00:00:06 Welcome to the Spoken Tutorial on Exception and error handling in PERL 2 00:00:06 --> 00:00:12 In this tutorial we will learn to Catch errors and Handle exceptions 3 00:00:12 --> 00:00:23 For this tutorial, I am using Ubuntu Linux 12.04 operating system Perl 5.14.2 and the gedit Text Editor 4 00:00:23 --> 00:00:27 You can use any text editor of your choice. 5 00:00:27 --> 00:00:32 To follow this tutorial, you should have working knowledge of Perl Programming. 6 00:00:32 --> 00:00:39 If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website. 7 00:00:39 --> 00:00:47 When an error occurs: Exception handling deviates the execution of a program from the normal execution path. 8 00:00:47 --> 00:00:53 Error handling helps to recover the program, without terminating the application. 9 00:00:53 --> 00:01:01 We can identify and trap an error in a number of ways. We will see few commonly used methods in Perl. 10 00:01:01 --> 00:01:07 The warn function only raises a warning message without taking further action. 11 00:01:07 --> 00:01:13 The die function immediately terminates the execution and displays the error message. 12 00:01:13 --> 00:01:20 Let us understand the die function using a sample program, which I have already saved. 13 00:01:20 --> 00:01:29 Go to the terminal and type gedit die dot pl ampersand and press Enter 14 00:01:29 --> 00:01:35 This is code in die.pl file. Let us understand the code now. 15 00:01:35 --> 00:01:46 Here we have defined a function divide which takes two input parameters i.e dollar numerator and dollar denominator 16 00:01:46 --> 00:01:53 At the rate underscore (@_) is a special variable used to pass the parameter list to the function. 17 00:01:53 --> 00:01:57 If the denominator is zero, the die function will quit the script. 18 00:01:57 --> 00:02:05 It will also display the error message for the user to read. Else it will print the output. 19 00:02:05 --> 00:02:08 These are the function call statements. 20 00:02:08 --> 00:02:15 The first two times, the function is executed because the second parameter is not zero. 21 00:02:15 --> 00:02:23 The third time, the denominator value is zero, so the die function is executed. 22 00:02:23 --> 00:02:29 The last divide function will not be executed as the die function quits the script. 23 00:02:29 --> 00:02:32 Press Ctrl + S to save the program. 24 00:02:32 --> 00:02:35 Let us execute the program. 25 00:02:35 --> 00:02:43 Switch back to the terminal and type, perl die dot pl and press Enter. 26 00:02:43 --> 00:02:49 The output is displayed as shown here. Can't divide by zero! - 27 00:02:49 --> 00:02:54 This is the error message we have given in the program in the die statement. 28 00:02:54 --> 00:03:00 Next, we will see how to use eval function in error handling. 29 00:03:00 --> 00:03:06 eval function is used for handling run-time errors or exceptions. 30 00:03:06 --> 00:03:14 For example, built-in errors such as out of memory, divide by zero or user defined errors. 31 00:03:14 --> 00:03:19 The general syntax for eval function is shown here. 32 00:03:19 --> 00:03:25 The dollar exclamation($!) special variable holds the error message, if any. 33 00:03:25 --> 00:03:33 Otherwise, dollar exclamation( $!) holds an empty string. That means it is evaluated as false. 34 00:03:33 --> 00:03:40 Let us understand the eval function using a sample program. Go to the terminal. 35 00:03:40 --> 00:03:47 Type gedit eval dot pl ampersand and press Enter 36 00:03:47 --> 00:03:54 In the eval dot pl file, type the following code as displayed on the screen. Let me explain the code now. 37 00:03:54 --> 00:04:05 Here in our example,open FILE invokes the die statement, if it has trouble in opening a file “test.dat” 38 00:04:05 --> 00:04:13 Perl gives the system error message from the last eval block to the variable dollar exclamation( $!) 39 00:04:13 --> 00:04:17 Press Ctrl + S to save the file. 40 00:04:17 --> 00:04:25 Switch back to the terminal and type, perl eval dot pl and press Enter. 41 00:04:25 --> 00:04:30 The system error message is displayed as shown here. 42 00:04:30 --> 00:04:40 Let us see another example. This time we will see an error message returned from eval function using $@ (dollar at the rate). 43 00:04:40 --> 00:04:44 Let us switch back to the eval dot pl file. 44 00:04:44 --> 00:04:48 Type the code as shown on the screen. 45 00:04:48 --> 00:04:56 We are passing $total, $count as input parameters to the function average. 46 00:04:56 --> 00:05:00 We have a possibility of getting an error if the count is zero. 47 00:05:00 --> 00:05:04 Here, that is handled with the die statement. 48 00:05:04 --> 00:05:11 The error message returned from eval is displayed using $@ ( dollar at the rate) 49 00:05:11 --> 00:05:15 If not, it will print the Average value. 50 00:05:15 --> 00:05:22 Press Ctrl +S to save the file. Let us execute the program. 51 00:05:22 --> 00:05:31 Switch back to the terminal and type, perl eval.pl and press Enter. 52 00:05:31 --> 00:05:35 The output is as shown here. 53 00:05:35 --> 00:05:41 This brings us to the end of this tutorial. Let us summarise. 54 00:05:41 --> 00:05:47 In this tutorial, we have learnt how to Catch errors and Handle exceptions 55 00:05:47 --> 00:05:57 As an assignment do the following. On your Linux machine, create a file emp.txt with 5 employee names. 56 00:05:57 --> 00:06:02 Change permission of emp.txt to READ only. 57 00:06:02 --> 00:06:10 Note: Go through the relevant Linux spoken tutorials on the spoken tutorial website for change permission option. 58 00:06:10 --> 00:06:19 Write a Perl program to open the emp.txt file in WRITE mode and add few employee names to it. 59 00:06:19 --> 00:06:26 Using "eval", print appropriate error message if open/write operation fails. 60 00:06:26 --> 00:06:33 The video at the following link summarises the Spoken Tutorial project. Please download and watch it. 61 00:06:33 --> 00:06:42 The Spoken Tutorial Project Team conducts workshops using spoken tutorials and gives certificates on passing online tests. 62 00:06:42 --> 00:06:46 For more details, please write to us. 63 00:06:46 --> 00:06:53 Spoken Tutorial project is funded by NMEICT, MHRD, Government of India. 64 00:06:53 --> 00:06:58 More information on this mission is available at this link. 65 00:06:58 --> 00:07:03 This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.