0% found this document useful (0 votes)
2 views7 pages

Exceptional Handing in Python

The document provides an overview of exception handling in Python, including the basic structure of try-except blocks, raising exceptions, and handling specific error types such as ZeroDivisionError and ValueError. It demonstrates various scenarios, including nested try-except blocks and catching multiple exceptions. The examples illustrate how to handle errors gracefully and ensure that certain code executes regardless of exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Exceptional Handing in Python

The document provides an overview of exception handling in Python, including the basic structure of try-except blocks, raising exceptions, and handling specific error types such as ZeroDivisionError and ValueError. It demonstrates various scenarios, including nested try-except blocks and catching multiple exceptions. The examples illustrate how to handle errors gracefully and ensure that certain code executes regardless of exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

exceptional handing in python

In [3]:

a=int(input("enter the number"))

b=int(input("enter the number"))

div=a/b

print(div)

enter the number4

enter the number2

2.0

basic structure of exceptional handling

try:

#code that may arise the exception

except exceptiontype:

#mentions the exception type occured for you

else:

#no exception normal print statement

finally:

#code that runs no matter what (option)

In [7]:

a=int(input("enter the number"))

b=int(input("enter the number"))

try:

div=a//b

except ZeroDivisionError:

print("the denominator should not be zero")

else:

print(div)

finally:

print("this block is always exceuted")

enter the number6


enter the number0

the denominator should not be zero

this block is always exceuted

raising an exception in the python program

In [10]:

def check_age(age):

if age<18:

raise ValueError("age must be atleast 18")

else:

print("age is valid")

try:

check_age(37)

except ValueError as e:

print(e)

age is valid

catching all exception with the particular exception

In [12]:

try:

result=10/int(input("enter a number"))

except exception as e:

print(f" ann error occured:{e}")

enter a numberabc

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

ValueError Traceback (most recent call last)

Cell In[12], line 2

1 try:

----> 2 result=10/int(input("enter a number"))

3 except exception as e:

ValueError: invalid literal for int() with base 10: 'abc'


During handling of the above exception, another exception occurred:

NameError Traceback (most recent call last)

Cell In[12], line 3

1 try:

2 result=10/int(input("enter a number"))

----> 3 except exception as e:

4 print(f" ann error occured:{e}")

NameError: name 'exception' is not defined

handling index error for the list

In [16]:

try:

my_list=[1,2,3]

print(my_list[4])

except IndexError:

print("error: index is out of range")

error: index is out of range

handling multiple exceptions in one except block

In [18]:

try:

x=int(input("enter a number"))

result=10/x

except(ValueError,ZeroDivisionError) as e:

print("an error ocured:{e}")

enter a number0

an error ocured:{e}

catching and re-raising exception

In [26]:

def divide(a,b):
try:

return a/b

except ZeroDivisionError as e:

print("caught a zeroDivision error")

raise

try:

divide(10,0)

except ZeroDivisionError:

print("handled in the outer block")

caught a zeroDivision error

handled in the outer block

nested try-except block

In [31]:

try:

try:

x=int(input("enter a number"))

result=8/x

except ValueError:

print("invalid input.not a number")

except ZeroDivisionError:

print("cannot divide by zero")

else:

print(result)

enter a number0

cannot divide by zero

exceptional handing in python


In [3]:
a=int(input("enter the number"))
b=int(input("enter the number"))
div=a/b
print(div)
enter the number4
enter the number2
2.0

basic structure of exceptional handling


try:
#code that may arise the exception
except exceptiontype:
#mentions the exception type occured for you
else:
#no exception normal print statement
finally:
#code that runs no matter what (option)
In [7]:
a=int(input("enter the number"))
b=int(input("enter the number"))
try:
div=a//b
except ZeroDivisionError:
print("the denominator should not be zero")
else:
print(div)
finally:
print("this block is always exceuted")

enter the number6


enter the number0
the denominator should not be zero
this block is always exceuted

raising an exception in the python program


In [10]:
def check_age(age):
if age<18:
raise ValueError("age must be atleast 18")
else:
print("age is valid")
try:
check_age(37)
except ValueError as e:
print(e)
age is valid

catching all exception with the particular


exception
In [12]:
try:
result=10/int(input("enter a number"))
except exception as e:
print(f" ann error occured:{e}")
enter a numberabc
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[12], line 2
1 try:
----> 2 result=10/int(input("enter a number"))
3 except exception as e:

ValueError: invalid literal for int() with base 10: 'abc'

During handling of the above exception, another exception occurred:

NameError Traceback (most recent call last)


Cell In[12], line 3
1 try:
2 result=10/int(input("enter a number"))
----> 3 except exception as e:
4 print(f" ann error occured:{e}")

NameError: name 'exception' is not defined

handling index error for the list


In [16]:
try:
my_list=[1,2,3]
print(my_list[4])
except IndexError:
print("error: index is out of range")
error: index is out of range

handling multiple exceptions in one except block


In [18]:
try:
x=int(input("enter a number"))
result=10/x
except(ValueError,ZeroDivisionError) as e:
print("an error ocured:{e}")
enter a number0
an error ocured:{e}

catching and re-raising exception


In [26]:
def divide(a,b):
try:
return a/b
except ZeroDivisionError as e:
print("caught a zeroDivision error")
raise
try:
divide(10,0)
except ZeroDivisionError:
print("handled in the outer block")

caught a zeroDivision error


handled in the outer block

nested try-except block


In [31]:
try:
try:
x=int(input("enter a number"))
result=8/x
except ValueError:
print("invalid input.not a number")
except ZeroDivisionError:
print("cannot divide by zero")
else:
print(result)
enter a number0
cannot divide by zero

You might also like