Exception Handling In Python

 

Python Exception Handling

Exception provides try and except block mechanism to handle exception
  • If any exception occurs in try block, try block will create object to occurred exception class and handover the object to except block with matching exception handler.
  • The except block is responsible to handle occurred exception.
  • We can define multiple exception blocks if we use named except block.
  • Types of except blocks
    • Named except block
                      This block can handle matching to the exception class name it is defined for.

    • Default except block
                    This block can handle all types of exception, but provide a single solution for all.


  • How we can handle exception
    • Try and except block
                       It is a combination of try and except, exception thrown in statement under try, will handover to except blog

    • Try with else block
                      It is a combination of try and except with else blog, exception thrown in statement under try, will handover to except blog and then else blog will be executed only in case of no exception thrown.

    • Try with finally block
                      It is a combination of try and except with finally blog , exception thrown in statement under try, will handover to except blog and finally blog will execute in both conditions, exception thrown or not







Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India