91-9990449935 0120-4256464 |
EXCEPTION HANDLINGException can be said to be any abnormal condition in a program resulting to the disruption in the flow of the program. Whenever an exception occurs the program halts the execution and thus further code is not executed. Thus exception is that error which python script is unable to tackle with. Exception in a code can also be handled. In case it is not handled, then the code is not executed further and hence execution stops when exception occurs. Hierarchy Of Exception:
etc.. Exception Handling:The suspicious code can be handled by using the try block. Enclose the code which raises an exception inside the try block. The try block is followed except statement. It is then further followed by statements which are executed during exception and in case if exception does not occur. Syntax: try: malicious code except Exception1: execute code except Exception2: execute code .... .... except ExceptionN: execute code else: In case of no exception, execute the else block code. eg: try: a=10/0 print a except ArithmeticError: print "This statement is raising an exception" else: print "Welcome" Output: >>> This statement is raising an exception >>> Explanation:
Except with no Exception:Except statement can also be used without specifying Exception. Syntax: try: code except: code to be executed in case exception occurs. else: code to be executed in case exception does not occur. eg: try: a=10/0; except: print "Arithmetic Exception" else: print "Successfully Done" Output: >>> Arithmetic Exception >>> Declaring Multiple ExceptionMultiple Exceptions can be declared using the same except statement: Syntax: try: code except Exception1,Exception2,Exception3,..,ExceptionN execute this code in case any Exception of these occur. else: execute code in case no exception occurred. eg: try: a=10/0; except ArithmeticError,StandardError: print "Arithmetic Exception" else: print "Successfully Done" Output: >>> Arithmetic Exception >>> Finally Block:In case if there is any code which the user want to be executed, whether exception occurs or not then that code can be placed inside the finally block. Finally block will always be executed irrespective of the exception. Syntax: try: Code finally: code which is must to be executed. eg: try: a=10/0; print "Exception occurred" finally: print "Code to be executed" Output: >>> Code to be executed Traceback (most recent call last): File "C:/Python27/noexception.py", line 2, in <module> a=10/0; ZeroDivisionError: integer division or modulo by zero >>> In the above example finally block is executed. Since exception is not handled therefore exception occurred and execution is stopped. Raise an Exception:You can explicitly throw an exception in Python using ?raise? statement. raise will cause an exception to occur and thus execution control will stop in case it is not handled. Syntax: raise Exception_class,<value> eg: try: a=10 print a raise NameError("Hello") except NameError as e: print "An exception occurred" print e Output: >>> 10 An exception occurred Hello >>> Explanation: i) To raise an exception, raise statement is used. It is followed by exception class name. ii) Exception can be provided with a value that can be given in the parenthesis. (here, Hello) iii) To access the value "as" keyword is used. "e" is used as a reference variable which stores the value of the exception. Custom Exception:Refer to this section after visiting Class and Object section: Creating your own Exception class or User Defined Exceptions are known as Custom Exception. eg: class ErrorInCode(Exception): def __init__(self, data): self.data = data def __str__(self): return repr(self.data) try: raise ErrorInCode(2000) except ErrorInCode as ae: print "Received error:", ae.data Output: >>> Received error : 2000 >>>
Next TopicPython Exception Handling
|