Download presentation
Presentation is loading. Please wait.
Published byAlexis McCoy Modified over 9 years ago
1
Exceptions 2 COMPSCI 105 S2 2015 Principles of Computer Science
2
Exception not Matched If no matching except block is found, the run-time system will attempt to handle the exception, by terminating the program. COMPSCI 1052 def divide(a, b): try: result = a / b except IndexError: result = 'Type of operands is incorrect' except ZeroDivisionError: result = 'Divided by zero' return result def divide(a, b): try: result = a / b except IndexError: result = 'Type of operands is incorrect' except ZeroDivisionError: result = 'Divided by zero' return result x = divide(5, "5") print(x) x = divide(5, "5") print(x) Traceback (most recent call last): File "...", line 10, in x = divide(5, '5') File "...", line 3, in divide result = a / b TypeError: unsupported operand type(s) for /: 'int' and 'str' Traceback (most recent call last): File "...", line 10, in x = divide(5, '5') File "...", line 3, in divide result = a / b TypeError: unsupported operand type(s) for /: 'int' and 'str' Lecture08
3
Order of except clauses Specific exception block must come before any of their general exception block COMPSCI 1053 def divide(a, b): try: result = a / b except: result = 'Type of operands is incorrect' except ZeroDivisionError: result = 'Divided by zero' return result def divide(a, b): try: result = a / b except: result = 'Type of operands is incorrect' except ZeroDivisionError: result = 'Divided by zero' return result File "...", line 3 result = a / b ^ SyntaxError: default 'except:' must be last File "...", line 3 result = a / b ^ SyntaxError: default 'except:' must be last Lecture08
4
Exceptions Any kind of built-in error can be caught Check the Python documentation for the complete list Some popular errors: ArithmeticError: various arithmetic errors ZeroDivisionError IndexError: a sequence subscript is out of range TypeError: inappropriate type ValueError: has the right type but an inappropriate value IOError: Raised when an I/O operation EOFError: hits an end-of-file condition (EOF) without reading any data …… https://docs.python.org/3/library/exceptions.html - exception-hierarchy https://docs.python.org/3/library/exceptions.html - exception-hierarchy COMPSCI 1054Lecture08
5
The Finally clause The finally block is optional, and is not frequently used Executed after the try and except blocks, but before the entire try-except ends Code within a finally block is guaranteed to be executed if any part of the associated try block is executed regardless of an exception being thrown or not It is intended to define cleanup actions that must be performed under all circumstances. COMPSCI 1055 try: statement block here except: more statements here finally: more statements here try: statement block here except: more statements here finally: more statements here Lecture08
6
The else clause Executed only if the try clause completes with no errors It is useful for code that must be executed if the try clause does not raise an exception. COMPSCI 1056 try: except :... except : except: else: finally: try: except :... except : except: else: finally: Lecture08
7
Example Try statement: Case 1: No error Case 2: Divided by zero COMPSCI 1057 def divide(a, b): try: result = a / b except ZeroDivisionError: result = 'Divided by zero' else: print("result is", result) finally: print("executing finally clause") return result def divide(a, b): try: result = a / b except ZeroDivisionError: result = 'Divided by zero' else: print("result is", result) finally: print("executing finally clause") return result Lecture08 x = divide(2, 1) print(x) x = divide(2, 1) print(x) result is 2.0 executing finally clause 2.0 result is 2.0 executing finally clause 2.0 x = divide(2, 0) print(x) x = divide(2, 0) print(x) executing finally clause Divided by zero executing finally clause Divided by zero Case 3: Error x = divide('2', '1') print(x) x = divide('2', '1') print(x) executing finally clause Traceback (most... TypeError: unsupported operand type(s)... executing finally clause Traceback (most... TypeError: unsupported operand type(s)...
8
Exercise 2 What is the output of the following program when x is 1, 0 and '0'? COMPSCI 1058 def testing(x): try: print('Trying some code') y = 2 / x except ZeroDivisionError: print('ZeroDivisionError raised here') except: print('Error raised here') else: print('Else clause') finally: print('Finally') def testing(x): try: print('Trying some code') y = 2 / x except ZeroDivisionError: print('ZeroDivisionError raised here') except: print('Error raised here') else: print('Else clause') finally: print('Finally') Lecture08
9
Raising an exception: You can create an exception by using the raise statement The program stops immediately after the raise statement; and any subsequent statements are not executed. It is normally used in testing and debugging purpose Example: COMPSCI 1059 def checkLevel(level): if level < 1: raise ValueError('Invalid level!') else: print (level) def checkLevel(level): if level < 1: raise ValueError('Invalid level!') else: print (level) raise Error('Error message goes here') Lecture08 Traceback (most recent call last):... raise ValueError('Invalid level!') ValueError: Invalid level! Traceback (most recent call last):... raise ValueError('Invalid level!') ValueError: Invalid level!
10
Example – Fraction revisited COMPSCI 10510 class Fraction: def __init__(self, top, bottom): if bottom == 0: raise ValueError("Denominator Zero") else: common = Fraction.__gcd(top, bottom) self.__num = top // common self.__den = bottom // common class Fraction: def __init__(self, top, bottom): if bottom == 0: raise ValueError("Denominator Zero") else: common = Fraction.__gcd(top, bottom) self.__num = top // common self.__den = bottom // common Lecture08 from Fraction import Fraction try: x = Fraction(2,3) print('Fraction 1 is :' + str(x)) y = Fraction(2,0) print('Fraction 2 is :' + str(y)) z = Fraction(1,4) print('Fraction 3 is :' + str(z)) Except ValueError: print('Invalid Denominator!') from Fraction import Fraction try: x = Fraction(2,3) print('Fraction 1 is :' + str(x)) y = Fraction(2,0) print('Fraction 2 is :' + str(y)) z = Fraction(1,4) print('Fraction 3 is :' + str(z)) Except ValueError: print('Invalid Denominator!') Fraction 1 is :2/3 Invalid Denominator! Fraction 1 is :2/3 Invalid Denominator! Saved in Fraction.py Saved in Example.py
11
Benefits of of using exception handling Separates the detection of an error (done in a called function) from the handling of an error (done in the calling method). Often the called function does not know what to do in case of an error. It throws an exception to its caller. The libirary function can detect the error, but only the caller knows what needs to be done when an error occurs. If an exception was not caught in the current function, it is passed to its caller. The process is repeated until the exception is caught or passed to the main function (either is handled or the program disrupts and terminates without further execution). Raise and handle exceptions 11 COMPSCI 105 Lecture08
12
An exception is wrapped in an object. To throw an exception, you first create an exception object and then use the raise keyword to throw it. This exception object can be access from the except clause with the following syntax. Example: Using exception objects 12 COMPSCI 105 Lecture08 try: except ExceptionType as ex: try: except ExceptionType as ex: try: x = Fraction(2,3) print('Fraction 1 is :' + str(x)) y = Fraction(2,0) print('Fraction 2 is :' + str(y)) Except ValueError as ex: print("Exception: "+ str(ex)) try: x = Fraction(2,3) print('Fraction 1 is :' + str(x)) y = Fraction(2,0) print('Fraction 2 is :' + str(y)) Except ValueError as ex: print("Exception: "+ str(ex)) Fraction 1 is :2/3 Exception: Denominator Zero Fraction 1 is :2/3 Exception: Denominator Zero
13
Handling Exceptions Put code that might create a runtime error is enclosed in a try block COMPSCI 10513 def checkLevel(level): if level < 1: raise ValueError('Invalid level!') else: print (level) try: checkLevel(-2) print ('This print statement will not be reached.') except ValueError as x: print ('Problem: {0}'.format(x)) def checkLevel(level): if level < 1: raise ValueError('Invalid level!') else: print (level) try: checkLevel(-2) print ('This print statement will not be reached.') except ValueError as x: print ('Problem: {0}'.format(x)) Lecture08 Problem: Invalid level!
14
When to use try catch blocks? If you are executing statements that you know are unsafe and you want the code to continue running anyway. When to raise an exception? When there is a problem that you can't deal with at that point in the code, and you want to "pass the buck" so the problem can be dealt with elsewhere. Using Exceptions 14COMPSCI 105 Lecture08
15
Exercise 3 Modify the following function that calculates the mean value of a list of numbers to ensure that the function generates an informative exception when input is unexpected. COMPSCI 10515 def mean(data): sum = 0 for element in data: sum += element mean = sum / len(data) return mean def mean(data): sum = 0 for element in data: sum += element mean = sum / len(data) return mean Lecture08
16
Summary Exceptions alter the flow of control When an exception is raised, execution stops When the exception is caught, execution starts again try… except blocks are used to handle problem code Can ensure that code fails gracefully Can ensure input is acceptable finally Executes code after the exception handling code COMPSCI 10516Lecture08
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.