Python Exceptions Handling
As in the beginning of this tutorial we have studied about the types of errors that occur in a program. Sometime we what to catch some or all errors that can possibly get generated; and as a programmer we want to be as specific as possible. So, python allows programmers to deal with errors smoothly.
Exceptions are events that are used to modify the flow of control through a program when the error occurs. Exceptions get triggered automatically on finding errors in Python.
These exceptions are processed using five statements. These are:
The last was an optional extension until Python 2.6 & Python 3.0.
Why Exceptions Are Used?
Exceptions allow us to jump out of random illogical large chunks of codes in case of errors. Let us take a scenario that you have given a function to do a specific task. If you go there and found those things missing that are required to do that specific task, what will you do? Either you stop working or think about a solution – where to find those items to perform the task. Same thing happens here in case of Exceptions also. Exceptions allow programmers jump to an exception handler in a single step, abandoning all function calls. You can think exceptions like an optimized quality go-to statement, in which the program error that occurs at runtime gets easily managed by the exception block. When the interpreter encounters error, it lets the execution go to the exception part to solve and continue the execution instead of stopping.
While dealing with exceptions, the exception handler creates a mark & executes some code. Somewhere further within that program the exception is raised that solves the problem & makes Python jump back to the marked location; by not throwing away/skipping any active functions that were called after the marker was left.
Roles Of An Exception Handler In Python
A Simple Program To Demonstrate Python Exception Handling
Example 01:
#!/usr/bin/python
(a,b) = (6,0)
try: # simple use of try-except block for handling errors
g = a/b
except ZeroDivisionError:
print ("This is a DIVIDED BY ZERO error")
The above program can also be written like this:
Example 02:
#!/usr/bin/python
(a,b) = (6,0)
try:
g = a/b
except ZeroDivisionError as s:
k = s
print (k)
#Output will be: integer division or modulo by zero
The ‘try – Except’ Clause With No Exception
The structure of such type of ‘except’ clause having no exception is shown with an example.
#!/usr/bin/python
try:
# all operations are done within this block.
. . . . . .
except:
# this block will get executed if any exception encounters.
. . . . . .
else:
# this block will get executed if no exception is found.
. . . . . .
All the exceptions get caught where there is try/except statement of this type. Good programmers use this technique of exception to make program fully executable.
‘except’ Clause With Multiple Exceptions
#!/usr/bin/python
try:
# all operations are done within this block.
. . . . . .
except ( Exception1 [, Exception2[,….Exception N ] ] ] ) :
# this block will get executed if any exception encounters from the above lists of exceptions.
. . . . . .
else:
# this block will get executed if no exception is found.
. . . . . .
The ‘try – Finally’ Clause
#!/usr/bin/python
try:
# all operations are done within this block.
. . . . . .
# if any exception encounters, this block may get skipped.
finally:
. . . . . .
# this block will definitely be executed.
Comments
Post a Comment