Python Language / Python Exception Handling

Exception means run time errors. Python handle these errors using the below syntax.

try:
#Try Block
except ExceptionType1:

# exception Block1
except ExceptionType2:

# exception Block2 except ExceptionType3:
# exception Block3

finally:
# finally Block


Explanation: first it will execute try block after then it checks the exception block if no exception found it will execute the finally block.

Program for exception handling Output

a=10
b=0
try:
  print(c)
  print(a/b)

except NameError:
  print("Variable C not defined")

except ZeroDivisionError:
  print("Zero Division Error")

finally:
  print("no Exception")

Variable x is not defined no Exception


Exception Name Raise due to
Exception function all exceptions.
StopIteration Iterator exceptions.
SystemExit sys.exit() function exceptions.
StandardError Built in exceptions.
ArithmeticError Mathematical calculation errors.
OverflowError Data type exceeds errors
FloatingPointError Raised when a floating point calculation fails.
ZeroDivisionError division / modulo by zero error
AssertionError Assert statement error
AttributeError Raised in case of failure of attribute reference or assignment.
EOFError File errors
ImportError Module errors
KeyboardInterrupt user interrupts program execution errors
LookupError lookup errors.
IndexError Index not found errors
KeyError Dictionary key errors.
NameError identifier errors
UnboundLocalError No value to variable error
EnvironmentError Python environment errors
IOError File errors
IOError System errors.
SyntaxError Python syntax errors.
IndentationError Spacing problems in the program
SystemError interpreter errors.
SystemExit sys.exit() errors
TypeError Function argument invalid data type.
ValueError invalid function arguments values specified.
RuntimeError Execution time errors
NotImplementedError abstract method errors.


Home     Back