PREV NEXT
1. Exception Handling is the mechanism it is allows to handle errors very smartly while the program is running.
2. Runtime erros is nothing but it happens while running the program,if it is runtime error it will no throughs any sytax of the program
Some of the common Exceptions:
1. IOError:It will happen when Input or output operation failed
2. EOFError:It will happen when the file has reached end point and still operations are goingon.
3. NameError:It will occur when name is not found.
4. ZeroDivisionError:It will happen a number is division by zero
5. IndentationError:When source code is not in indenet
Syntax:
1. Single Exception Handling:
try:
suspicious code
except exception:
exception statement
2. Multiple Exceptions:
try:
suspicious code
except exception1:
exception1 statement
except exception2:
exception2 statement
except exception3:
exception3 statement
3. Try—exception with else clause
A try—exception statement also can have an optional else clause ,it is excute only when no exception has been occured.
Syntax:
try:
suspicious code
except exception1:
exception1 statement
except exception2:
exception2 statement
except exception3:
exception3 statement
else:
statement
4. Try—exception with finally clause
A try—exception statement also can have an optional finally clause ,it is always excute wheathe exception has been occured or not.
Syntax:
try:
suspicious code
except exception1:
exception1 statement
except exception2:
exception2 statement
except exception3:
exception3 statement
finally:
statement
5. Raise an exception
We can explicitly raise an exception by usng raise statement ,it will be cause an exception to occur and it will not handle in case of execution control will stop.
Syntax:
raise exception_class,data
Example1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
a=int(input("Enter a Number1:")) b=int(input("Enter a Number2:")) try: res=a/b print ("Result is:",res) except ZeroDivisionError: print ("ZeroDivisionError is raising an exception") <strong> </strong> |
Iteration1 Iteration2
Input: Input:
1 2 3 |
Enter a number1: 10 Enter a number1: 10 Enter a number1: 5 Enter a number1: 0 |
Output: Output:
1 |
Result is: 2.0 ZeroDivisionError is raising an exception |
Example2:
try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
value1 = int(input("Enter a number1: ")) value2 = int(input("Enter a number2: ")) result = value1 / value2 print("Result is: ", result) except ValueError: print("ValueError:Exception Handler") print("Invalid input: Only integers are allowed") except ZeroDivisionError: print("ZeroDivisionError:Exception Handler") print("OH!!!! divide a number by 0") except IndentationError: print("IndentationError:Exception Handler") |
Iteration1 Iteration2
Input: Input:
1 2 3 |
Enter a number1: 10 Enter a number1: 10 Enter a number1: 5 Enter a number1: 0 |
Output: Output:
1 2 3 |
Result is: 2.0 ZeroDivisionError:Exception Handler OH!!!! divide a number by 0 |
Iteration3
Input
1 2 3 |
Enter a number1: 10 Enter a number1: “5” |
Output:
1 2 3 |
ValueError:Exception Handler Invalid input: Only integers are allowed |
Example3:
try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
value1 = int(input("Enter a number1: ")) value2 = int(input("Enter a number2: ")) result = value1 / value2 print("Result is: ", result) except ValueError: print("ValueError:Exception Handler") print("Invalid input: Only integers are allowed") except ZeroDivisionError: print("ZeroDivisionError:Exception Handler") print("OH!!!! divide a number by 0") except IndentationError: print("IndentationError:Exception Handler") else: print("Program successfully executed") |
Input
1 2 3 |
Enter a number1: 10 Enter a number1: 5 |
Output:
1 2 3 |
Result is: 2.0 Program successfully executed |
Example4:
try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
value1 = int(input("Enter a number1: ")) value2 = int(input("Enter a number2: ")) result = value1 / value2 print("Result is: ", result) except ValueError: print("ValueError:Exception Handler") print("Invalid input: Only integers are allowed") except ZeroDivisionError: print("ZeroDivisionError:Exception Handler") print("OH!!!! divide a number by 0") except IndentationError: print("IndentationError:Exception Handler") finally: print("This is Final Clause") |
Iteration1 Iteration2
Input: Input:
1 2 3 |
Enter a number1: 10 Enter a number1: 10 Enter a number1: 5 Enter a number1: 0py |
Output: Output:
1 2 3 4 5 |
Result is: 2.0 ZeroDivisionError:Exception Handler This is Final Clause OH!!!! divide a number by 0 This is Final Clause |
Iteration3
Input
1 2 3 |
Enter a number1: 10 Enter a number1: “5” |
Output:
1 2 3 4 5 |
ValueError:Exception Handler Invalid input: Only integers are allowed This is Final Clause |