programming inbbm101/fall18/files/... · 2018-12-14 · programming in hacettepe university...
TRANSCRIPT
Programming in
Hacettepe University
Computer Engineering Department
BBM103 Introduction to Programming Lab 1
Week 10
Fall 2018
Exceptions
Built-in ExceptionsThe simplest way to handle exceptions is with a "try-except" block:
Output: divide by zero
Example 1:
Example 2: except ValueError:
Example 3: except ZeroDivisionError:
Example 4: except (ValueError, ZeroDivisionError)
Example 5: try... except... as...
Example 6: try... except... else...
Example 7: try... except... finally...
Some Examples using Exceptionsexcept IOError:
print('An error occured trying to read the file.')
except ValueError: print('Non-numeric data found in the file.')
except ImportError: print ("NO module found«)
except EOFError: print('Why did you do an EOF on me?')
except KeyboardInterrupt:print('You cancelled the operation.')
except: print('An error occured.')
raiseExample 8:
Example 9:
Example 10: User-Defined exceptions
This example continues in the next slide
Example 10 continued
Assert
Output:
Example 11:
assert <some_test>, <message>
Example 12:
Output:
Example 13:
Output: