Catch exception throwed inside a function : Catch Exceptions « Language Basics « Python






Catch exception throwed inside a function

Catch exception throwed inside a function

def f():
    print "in f, before 1/0"
    1/0                           # raises a ZeroDivisionError exception
    print "in f, after 1/0"

def g():
    print "in g, before f()"
    f()
    print "in g, after f()"

def h():
    print "in h, before g()"


try:
    g()
    print "in h, after g()"
except ZeroDivisionError:
    print "ZD exception caught"
print "function h ends"

           
       








Related examples in the same category

1.Catch several exceptionsCatch several exceptions
2.Catch and recover exceptionCatch and recover exception