Today’s Agenda
Exception handling contd
Exception handling contd
Let us see how exception handling mechanism works when multiple
method calls are involved through the following example
1 Python fundamental| Rooman Technologies
Output:
Stack
ZeroDivisionError fun2()
num 100
Traceback
den 0
res 100/0
ZeroDivisionError
fun1()
ZeroDivisionError
main()
Abrupt termination
2 Python fundamental| Rooman Technologies
If except block is not in the method where exception is generated
then it traces back to the method who called for it and checks in
that method. If anywhere it is not present then the program is
abruptly terminated.
Let us try to handle this exception
Output:
3 Python fundamental| Rooman Technologies
Stack
ZeroDivisionError fun2()
num 100
den 0
Execution completes res 100/0
fun1()
main()
Let us see what happens when the except block is in fun1()
4 Python fundamental| Rooman Technologies
Output:
We can see that exception is been handled, but fun2() did not finish
it’s execution as the control came to fun1() along with exception
object.
Let us also try placing the except block in main()
5 Python fundamental| Rooman Technologies
Output:
As we see exception is handled but fun1() and fun2() did not finish
their execution.
6 Python fundamental| Rooman Technologies