In this article we are going to cover Python Exception Handling with Examples, List of Exception errors in Python, Example of raise the exception with message in Python.
The try blocks lets you test a block of code for errors.
The except block lets you handle the error.
The finally block lets you execute code, regardless of the result of the try- and except blocks.
Exception = Error
Exception are abnormal termination of program this occurred abruptly during runtime and stop the exception of the program.
There are two types of exception:-
- System- defined exception.
- User – defined exception.
Python Exception Handling:
It is an abnormal event which occurs at the runtime and suspends the execution.
Python having the capability for managing the execution.
Exception can be handled in python using try except keyword.
Errors are of two types
- Compile errors
- Logical errors
List of Exception errors in Python:
- IOError : If file can’t opened.
- KeyboardInterrupt : When an unrequired key is pressed by the user.
- ValueError : When built-in function receives a wrong argument.
- EOFError : If End-Of-File is hit without reading any data.
- ImportError : If it is unable to find the module.
The critical operation which can raise an exception is placed inside the try clause. The code that handles the exception is written in the except clause.
try: //statements except: //statements
Example of exceptions can be handled using the try statement in Python:
try: print(a) except: print("An exception occurred")
An exception occurred
The try block will generate an exception, because a is not defined:
No Exception, so try clause will run
def add(a, b): try: result = a + b print("Your answer is :", result) except ZeroDivisionError: print("Sorry ! You have an Error ") add(3, 2)
Your answer is : 5
There is an exception so except clause will run.
def divide(a, b): try: result = a / b print("Yeah ! Your answer is :", result) except ZeroDivisionError: print("Sorry ! You are dividing by zero ") divide(3, 0)
Sorry ! You are dividing by zero
The try block does not generate any error so else block is executed
try: print("Hello world") except: print("Something went wrong") else: print("Nothing went wrong")
Hello world Nothing went wrong
The finally block gets executed no matter if the try block raises any errors:
Example of finally block in Python:
try: print(a) except: print("Something went wrong") finally: print("The 'try block and except block' is finished")
Example of Raising Exception for predefined condition:
try: age = 17 if age <= 18: raise ValueError("You are not Eligible") else: print("You are Eligible") except ValueError as e: print(e)
You are not Eligible
Example of raise the exception with message in Python:
try: num = int(input("Enter a positive integer: ")) if(num <= 0): raise ValueError("That is a negative number!") except ValueError as e: print(e)
Enter a positive integer: -9 That is a negative number!
try: a = int(input("Enter a:")) b = int(input("Enter b:")) if b is 0: raise ArithmeticError else: print("a/b = ",a/b) except ArithmeticError: print("The value of b can't be 0")
========= Enter a:10 Enter b:10 a/b = 1.0 or Enter a:10 Enter b:0 The value of b can't be 0
We have covered Python Exception Handling with Examples.