The exception argument

When you write a program, it is very mundane and tedious to write each and every exception type. Instead of writing each exception, you could use just one line. See the code in exceptiontype.py:

try: 
num = int(raw_input("Enter the number "))
re = 100/num
print re
except Exception as e :
print e, type(e)

The preceding code catches the exception as e and type(e) displays its exception type. Let's see the output. In the code, e is the argument of the exception: the contents referred by the argument vary by exception:

Output of program exceptiontype.py

You can see that we run the program three times. The first time we passed the value 10. It runs successfully. In the second run, we passed string 'mohit', which is when the interpreter handled the exception and showed the exception message and its type. In third run, we passed 0, which is when interpreter showed the corresponding exception message and its type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.159.223