Exception Handling
The process of finding errors in a program is termed as Debugging.
There are three types of erros:
1. Syntax Error
2. Run time error
3. Logical Error
Errors are exceptional, unusual and unexpected situations and they are never part of the normal flow of a program.
In Python, such unusual situations are termed as Exceptions. Exceptions are usually run-time errors.
Exception Handling: Like any programming languages, Python also provides a complete mechanism for handling an error when it occurs. This mechanism is called Exception Handling.
Exception: An exception can be described as an interrupt or an event triggered during the normal flow of the execution of a program, which is to be handled carefully.
Some important terms related to Exception Handling:
(i) Traceback: The lengthy error message that is shown when an exception occurs during the program run is called a traceback.
(ii) Try block: Try block constitutes a set of codes that might have an exception thrown in it.
(iii) ‘except’ block or ‘catch’ block: Whenever some error or exception is to be handled, it is done using ‘except’ block which stands for exception.
(iv) ‘throw’ or ‘raise’ block: ‘throw’ is an action in response to an exception (error/unusual condition).
(v) Unhandled, uncaught Exception: An exception that leads to abnormal termination of a program due to non-execution of an exception thrown is termed as unhandled or uncaught exception.
For handling exceptional situations Python provides—
1. raise statement to raise exception in the program.
2. try... except statement for catching and handling errors
Example 1:
try:
x = 10
y = 20
print(x,y)
except:
print("Error")
Output:
10,20
There is no run time error hence there is no Error message.
Example 2:
try:
x = 10
y = 20
print(x,y,z)
except:
print("Error")
Output:
Error
Since there is run time error hence Error message is printed.
Some standard built-in exceptions:
Exception Description
NameError Raised when an identifier is not found
TypeError Raised when an operation or function is attempted that is invalid.
ZeroDivisionError Raised when division by zero takes place
KeyError Raised when dictionary key is not found
IndentationError Raised due to incorrect indentation
IOError Raised if the file requested cannot be opened
IndexError Raised when an index is not found in a sequence
ImportError Raised if Python cannot find the module requested
EOFError Raised when one of the file methods, (i.e., read(), readline() or readlines()), tries to read beyond the file.
SyntaxError Raised when there is an error in Python syntax
RuntimeError Raised when an error does not fall under any specific exception category defined above.
Different methods for handling exceptions:
Method 1:
try ... except
Method 2:
try...except with else block
try:
#Run this code
except:
#Execute this code when there is an exception
else:
#No exceptions? Run this code
Example:
num1=0
while num1 != 100:
try:
num1=eval(input("Enter dividend "))
num2=eval(input("Enter diviser "))
num3=num1//num2
print("The quotient is= ",num3)
except NameError: print("Variable not present")
except ZeroDivisionError: print("Divide by zero error ")
else:
print("Normal execution. Done!!")
Method 3: try...except with finally block
try:
# Run this code
except:
# Execute this code when there is an exception
else:
# No exception? Run this code
finally:
# Always run this code
No comments:
Post a Comment