Exception Handling in Python

Previous
Next

Exception Handling:

  • Exception is a “class”
  • Exception is a “Runtime Error”
  • When exception rises, the flow of execution will be terminated with informal information to the user.
class Add:
    def main():
        print("Enter 2 integers :")
        x = int(input())
        y = int(input())
        z = x+y
        print("Sum : ", z)
        print("End...")
        return

Add.main()
  • Exception handling is the concept of working with failure cases of logic.
  • As a programmer, we need to analyze success cases and failure cases in the transaction to implement the code.

Default Exception handler:

  • It is a pre-defined program.
  • When exception raises, an object will be created with the complete information of that Error is called “Exception Object”.
  • It is recommended to handle every exception in the application.
  • If we don’t handle, the object will be transferred to “Default Exception Handler”

Handling Exception:

  • Python library is providing pre-defined keywords to handle exception.
  • “try” block is used to place the doubtful code that raises exception.
  • If any exception has risen in the try block, we need to collect the exception object in “except” block to handle.
  • We collect the object reference by specifying Exception(class) type variable.
try :
	->Doubtful code…
	->Related code
except  <Exception_type>  var :
	->Handling logic…

Previous
Next

Add Comment

Courses Enquiry Form