Exception handling Examples in Python

7 / 100 SEO Score

Definition :

   It is a error occurs during run time of the program.

Error :

     External program when your block the execution program to the system. There are two types of exceptions is there

  • Compile time exceptions
  • Run time exceptions

Hierarchie of run time exceptions :

Exception handlers :

   Exception handlers are the keywords which is meant for holding the exception during run time of the program.

Types of exception handlers :

Try :

    This is a block of code which is meant for monitoring the statements for exceptions.

Try will transfer the control to the except or finally block when exception has been encountered in the program.

Except :

     Except is a block of code which is meant for taking one parameter that is an exception class for handling the exception returned by the try block

For one try block at least one except or finally block need to be there.

Program :

try:

a=10

b=’e’

c=a/b

print(c)

except ArithmeticError :

print(“ArithmeticError Occured”)

except TypeError :

print(“TypeError Occured”)

except Exception :

print(“Some Error Occured”)

Finally :

       Finally is a block of code which gets executed exactly once in the program life cycle. Finally will be executed either exception is there. If it is not there also.

Program 1 :

try:

a=10

b=’e’

c=a/b

print(c)

except ArithmeticError :

print(“ArithmeticError Occured”)

except TypeError :

print(“TypeError Occured”)

except Exception :

print(“Some Error Occured”)

finally :

print(“End”)

Try will work without except also

Program 2 :

try:

a=10

b=0

c=a/b

print(c)

finally:

print(“End of the program”)

Leave a Reply

Your email address will not be published. Required fields are marked *