Python - Exceptions

Python - Exceptions


Use of exceptions is to have a robust program and avoid any unintended termination of program execution. 
  • Each exception is represented as class in Python
  • Python provides built-in exceptions
  • users can define their own user-defined exceptions
  • BaseException is the base class for all the Python built-in exceptions
  • Exception is the sub class of BaseException

The sample program for the exception is

ls = ['a', 'b','45','77']

try:
    print(ls[4])
except Exception as k:
    print(k)
finally:
    print("Hello")
  • The statements inside the except block are called handlers
  • The statement inside the finally block are executed irrespective of the exception
  • finally is needed for proper closure of the except block

Post a Comment

0 Comments