0% found this document useful (0 votes)
33 views3 pages

Class 37 Exception Handeling 2

Uploaded by

Rane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

Class 37 Exception Handeling 2

Uploaded by

Rane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exception class_2

Every Exception in python is a class.

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

without try block:

print('Stmnt_1')
print(10/0)
print('Stmnt_3')

output:-

Stmnt_1

Traceback (most recent call last):


File "C:\Users\chand\PycharmProjects\pythonProject#13\Exception
Handeling\try_and_except_block.py", line 2, in <module>
print(10/0)
ZeroDivisionError: division by zero

Abnolrmal Termination/Non-Graceful Termination.

with try block:-

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

Control Flow in try-except:


--------------------------

try:
stmt_1
stmt_2
stmt_3

except ErrorName:
stmt_4

stmt_5

case_1:-If there is no exception.


print(1,2,3,5)

case_2:- If an exception raised at stmt_2 and correspomding except blocke matched.


print(1,4,5)

case_3:-If an exception raised at stmt_2 and creesponding except block not matched.
print(1)------Abnormal Termination.

try-with multiple except blocks:-


---------------------------------
The way of handeling exception is varied from exception to exception.Hence for
every exception type a seperate except block we have to provide that is try with
multiple except block is posiible and recommeded to use.

try:
-----
----
-----
except ZeroDivisonError:
--------
-------
except FileNotFoundError:
--------------
--------------
--------------

x = eval(input('Enetr your First Number:'))


y = input('Enter your second Number:')
print('Stmnt_1')
try:
print(x/y)
except ZeroDivisionError:
print('Error:-The cause of error is int value divide by zero')
except TypeError:
print('We can not divide int with str')
except Exception:
print('May be some error is happening.')

print('Stmnt_3')

single except block that can handel multiple exception:

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)

except (ZeroDivisionError,ValueError) as msg:


print('Plz provide valid numbers only and problem is:',msg)

Note:- Parenthesis are mandatory and this group of exceptions iternally considered
as tuple.

You might also like