Class 37 Exception Handeling 2
Class 37 Exception Handeling 2
All exception classes are child classes of BaseException that is every exception
class extends BaseException either directly or indirectly.Hence BaseException acts
as root for python Exception Hierachy.
syntax:
try:
Risky Code
except ErrorName:
Handeling code/Alternative Code or way
print('Stmnt_1')
print(10/0)
print('Stmnt_3')
output:-
Stmnt_1
print('Stmnt_1')
try:
print(10/0)
except ZeroDivisionError:
print('Error:-The cause of error is int value divide by zero')
print(10/2)
print('Stmnt_3')
output:-
Stmnt_1
Error:-The cause of error is int value divide by zero
5.0
Stmnt_3
try:
stmt_1
stmt_2
stmt_3
except ErrorName:
stmt_4
stmt_5
case_3:-If an exception raised at stmt_2 and creesponding except block not matched.
print(1)------Abnormal Termination.
try:
-----
----
-----
except ZeroDivisonError:
--------
-------
except FileNotFoundError:
--------------
--------------
--------------
print('Stmnt_3')
we can write a single except block that can handel multiple different types of
exceptions.
except(Exception1,Exception2,Exception3): or
except(Exception1,Exception2,Exception3) as msg:
try:
x = int(input('Enter your fisrt number:'))
y = int(input('Enter your second number:'))
print(x/y)
Note:- Parenthesis are mandatory and this group of exceptions iternally considered
as tuple.