PERL/C3/Exception-and-error-handling-in-PERL/Gujarati

From Script | Spoken-Tutorial
Revision as of 16:29, 1 February 2016 by Jyotisolanki (Talk | contribs)

Jump to: navigation, search
Time
Narration
00:01 PERL માં Exception and error handling પરના આ સ્પોકન ટ્યુટોરીયલમાં સ્વાગત છે.
00:06 આ ટ્યુટોરીયલમાં આપણે આપણે શીખીશું:
  • એરર Catch કરવું અને
  • exceptions. ને હેન્ડલ કરતા.
00:12 આ ટ્યુટોરીયલ માટે હું ઉપયોગ કરી રહી છું:
  • Ubuntu Linux 12.04 ઓપરેટીંગ સીસ્ટમ
  • Perl 5.14.2 અને
  • gedit ટેક્સ્ટ એડિટર
00:23 તમે તમારી પસંદગી અનુસાર કોઈ પણ ટેક્સ્ટ એડિટર વાપરી શકો છો.
00:27 આ ટ્યુટોરીયલના અનુસરણ માટે તમને Perl પ્રોગ્રામિંગ વિષે સામાન્ય જાણકારી હોવી જોઈએ.
00:32 જો નથી તો સ્પોકન ટ્યુટોરિયલ વેબ સાઈટ પર ઉપલબ્ધ Perl ટ્યુટોરિયલ જુઓ.
00:39 જયારે એક એરર આવે છે, તો Exception handling પ્રોગ્રામના એક્ઝેક્યુશન ને સામાન્ય એક્ઝેક્યુશન પાથ થી વિચલિત કરે છે.
00:47 એરર હેન્ડલિંગ એપ્લીકેશન ને બંદ કર્યા વગર પ્રોગ્રામ ને ફરી: મેળવવા માટે મદદ કરે છે.
00:53 આપણે અમુક રીતે એક એરરને ઓળખી અને ત્રેપ એટલેકે પકડી શકીએ છીએ . આપણે અમુક સામાન્ય ઉપયોગ થવા વાળા મેથડસ ને જોશું.
01:01

warn ફંકશન આગળની ક્રિયા લીધા વગર એક વોર્નિગ મેસેજ રેઈઝ કરે છે.

01:07 The die ફંકશન તરતજ એક્ઝેક્યુશન ને બંદ કરે છે અને એરર મેસેજ દેખાડે છે.
01:13 હવે એક સેમ્પલ પ્રોગ્રામ જે આપણે પહેલાથી જ સેવ કર્યો છે તેને ઉપયોગ કરીને die ફંકશન ને સમઝીએ.
01:20 terminal and type: ટર્મિનલ પર જાવ અને ટાઈપ કરો : gedit die dot pl ampersand અને Enter દબાવો.
01:29 અહી 'die.pl' ફાઈલમાં કોડ છે. ચાલો હવે કોડ સમઝીએ.
01:35 અહી આપણે divide નામક એક ફંકશન ને વ્યાખ્યાયિત કર્યું છે જે બે ઈનપુટ પેરા મીટરસ લે છે જે dollar numerator અને dollar denominator છે.
01:46 At the rate underscore (@_) એક સ્પેસિલ વેરીએબલ છે જે ફંકશન પર પેરામીટરની યાદીને પાસ કરવા માટે ઉપયોગ થાય છે.
01:53 જો denominator ઝીરો છે તો die ફંકશન સ્ક્રીપ્ટને છોડી દેશે .
01:57 આ યુઝરને વાંચવા માટે error message પણ દેખાડશે નહિતર આ આઉટપુટ પ્રિન્ટ કરશે.
02:05 function call સ્ટેટમેન્ટ છે.
02:08 પ્રથમ બે વખતે , ફંકશન એક્ઝીક્યુટ થાય છે કારણકે બીજો પેરામીટર ઝીરો નથી.
02:15 ત્રીજી વખતે denominator વેલ્યુ ઝીરો છે , તો die ફંકશન ઝીરો છે.
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

Jyotisolanki, PoojaMoolya