Question:
Write a function to compute 1 /0 and use try/except to catch the exceptions.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def throw(): return 1/0 try: throw() except ZeroDivisionError: print "Division by zero!" except Exception, err: print 'Caught an exception' finally: print 'finally' |
Explanation:
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.
A try
statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try
statement. An except clause may name multiple exceptions as a parenthesized tuple.
Output:
1 2 |
Division by zero! finally |
Leave a Reply