Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
Typing Speed Week Target Achieved 1 25 23 2 30 26 3 30 28 4 35 32 5 40 38
Jobs Applied # 1 2 3 Company Designation Applied Date Current Status
Python Exceptions Handling shafeeque ● shafeequemonp@gmail.com ● www.facebook.com/shafeequemonppambodan ● twitter.com/shafeequemonp ● in.linkedin.com/in/shafeequemonp ● 9809611325
What is Exception? ● ● An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions When a method encounters an abnormal condition (an exception condition) that it can't handle itself, it may throw an exception. Exception classes process ● In Java, exceptions are objects ● When you throw an exception, you throw an object ● ● Throwable serves as the base class for an entire family of classes, declared in java Throwable has two direct subclasses -Exception -Error
● ● ● ● ● Exceptions (members of the Exception family) are thrown to signal abnormal conditions that can often be handled by some catcher Though it's possible they may not be caught and therefore could result in a dead thread. Errors (members of the Error family) are usually thrown for more serious problems, such as OutOfMemoryError In general, code you write should throw only exceptions, not errors. Errors are usually thrown by the methods of the Java API, or by the Java virtual machine itself
Catching Exceptions: ● A method catches an exception using a combination othe try and catch keywords ● A try/catch block is placed around the code that might generate an exception. ● Code within a try/catch block is referred to as protected code ● Syntax: ● try { //Protected code } catch(ExceptionName e1) { //Catch block }
// File Name : ExcepTest.java import java.io.*; public class ExcepTest{ public static void main(String args[]){ try{ int a[] = new int[2]; System.out.println("Access element three :" + a[3]); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown :" + e); } System.out.println("Out of the block"); } } Output: Exception thrown Out of the block :java.lang.ArrayIndexOutOfBoundsException: 3
Errors and Exceptions: Syntax Errors ● ● It is also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python >>> while True print 'Hello world' File "<stdin>", line 1, in ? while True print 'Hello world' ^ SyntaxError: invalid syntax ● ● ● The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected The error is caused by (or at least detected at) the token preceding the arrow: in the example, the error is detected at the keyword print, since a colon (':') is missing before it File name and line number are printed so you know where to look in case the input came from a script.
Exceptions: ● Exceptions should be class objects ● The exceptions are defined in the module exceptions ● ● ● For class exceptions, in a try statement with an except clause that mentions a particular class Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it Errors detected during execution are called exceptions and are not unconditionally fatal
● >>> 10 * (1/0) Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: integer division or modulo by zero >>> 4 + spam*3 Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'spam' is not defined >>> '2' + 2 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects
Handling Exceptions: . >>> while True: ... try: ... x = int(raw_input("Please enter a number: ")) ... break ... ... except ValueError: print "Oops! That was no valid number. Try again..." ● The try statement works as follows: ● First, the try clause (the statement(s) between the try and except keywords) is executed. ● ● ● If no exception occurs, the except clause is skipped and execution of the try statement is finished. If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement. If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.
Raising Exceptions: ● ● The raise statement allows the programmer to force a specified exception to occur >>> raise NameError('HiThere') Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: HiThere ● ● If you need to determine whether an exception was raised but don’t intend to handle it, a simpler form of the raise statement allows you to reraise the exception: >>> try: ... raise NameError('HiThere') ... except NameError: ... print 'An exception flew by!' ... raise ... An exception flew by! Traceback (most recent call last): File "<stdin>", line 2, in ? NameError: HiThere
If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550

Exception handling in python

  • 2.
    Disclaimer: This presentationis prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3.
  • 4.
  • 5.
  • 6.
    What is Exception? ● ● Anexception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions When a method encounters an abnormal condition (an exception condition) that it can't handle itself, it may throw an exception. Exception classes process ● In Java, exceptions are objects ● When you throw an exception, you throw an object ● ● Throwable serves as the base class for an entire family of classes, declared in java Throwable has two direct subclasses -Exception -Error
  • 7.
    ● ● ● ● ● Exceptions (members ofthe Exception family) are thrown to signal abnormal conditions that can often be handled by some catcher Though it's possible they may not be caught and therefore could result in a dead thread. Errors (members of the Error family) are usually thrown for more serious problems, such as OutOfMemoryError In general, code you write should throw only exceptions, not errors. Errors are usually thrown by the methods of the Java API, or by the Java virtual machine itself
  • 8.
    Catching Exceptions: ● A methodcatches an exception using a combination othe try and catch keywords ● A try/catch block is placed around the code that might generate an exception. ● Code within a try/catch block is referred to as protected code ● Syntax: ● try { //Protected code } catch(ExceptionName e1) { //Catch block }
  • 9.
    // File Name: ExcepTest.java import java.io.*; public class ExcepTest{ public static void main(String args[]){ try{ int a[] = new int[2]; System.out.println("Access element three :" + a[3]); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown :" + e); } System.out.println("Out of the block"); } } Output: Exception thrown Out of the block :java.lang.ArrayIndexOutOfBoundsException: 3
  • 10.
    Errors and Exceptions: SyntaxErrors ● ● It is also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python >>> while True print 'Hello world' File "<stdin>", line 1, in ? while True print 'Hello world' ^ SyntaxError: invalid syntax ● ● ● The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected The error is caused by (or at least detected at) the token preceding the arrow: in the example, the error is detected at the keyword print, since a colon (':') is missing before it File name and line number are printed so you know where to look in case the input came from a script.
  • 11.
    Exceptions: ● Exceptions should beclass objects ● The exceptions are defined in the module exceptions ● ● ● For class exceptions, in a try statement with an except clause that mentions a particular class Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it Errors detected during execution are called exceptions and are not unconditionally fatal
  • 12.
    ● >>> 10 *(1/0) Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: integer division or modulo by zero >>> 4 + spam*3 Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'spam' is not defined >>> '2' + 2 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects
  • 13.
    Handling Exceptions: . >>> whileTrue: ... try: ... x = int(raw_input("Please enter a number: ")) ... break ... ... except ValueError: print "Oops! That was no valid number. Try again..." ● The try statement works as follows: ● First, the try clause (the statement(s) between the try and except keywords) is executed. ● ● ● If no exception occurs, the except clause is skipped and execution of the try statement is finished. If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement. If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.
  • 14.
    Raising Exceptions: ● ● The raisestatement allows the programmer to force a specified exception to occur >>> raise NameError('HiThere') Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: HiThere ● ● If you need to determine whether an exception was raised but don’t intend to handle it, a simpler form of the raise statement allows you to reraise the exception: >>> try: ... raise NameError('HiThere') ... except NameError: ... print 'An exception flew by!' ... raise ... An exception flew by! Traceback (most recent call last): File "<stdin>", line 2, in ? NameError: HiThere
  • 18.
    If this presentationhelped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 19.
    Contact Us Emarald Mall(Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550

Editor's Notes