exception handling in python
EXCEPTION HANDLING IN PYTHON
Exceptions versus Syntax Errors
Syntax errors occur when the parser detects an incorrect statement. Observe the following example:>>> print( 0 / 0 ))
File "<stdin>", line 1
print( 0 / 0 ))
^
SyntaxError: invalid syntax
The arrow indicates where the parser ran into the syntax error. In this example, there was one bracket too many. Remove it and run your code again:>>> print( 0 / 0 )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
Instead of showing the message
exception error
, Python details what type of exception error was encountered. In this case, it was a ZeroDivisionError
. Python comes with various built-in exceptions as well as the possibility to create self-defined exceptions.
Raising an Exception
We can useraise
to throw an exception if a condition occurs. The statement can be complemented with a custom exception.If you want to throw an error when a certain condition occurs using
raise
, you could go about it like this:x = 10
if x > 5:
raise Exception('x should not exceed 5. The value of x was: {}'.format(x))
Traceback (most recent call last):
File "<input>", line 4, in <module>
Exception: x should not exceed 5. The value of x was: 10
The program comes to a halt and displays our exception to screen, offering clues about what went wrong.
The AssertionError
Exception
Instead of waiting for a program to crash midway, you can also start by making an assertion in Python. We assert
that a certain condition is met. If this condition turns out to be True
, then that is excellent! The program can continue. If the condition turns out to be False
, you can have the program throw an AssertionError
exception.Have a look at the following example, where it is asserted that the code will be executed on a Linux system:
import sys
assert ('linux' in sys.platform), "This code runs on Linux only."
False
and the result would be the following:Traceback (most recent call last):
File "<input>", line 2, in <module>
AssertionError: This code runs on Linux only.
In this example, throwing an AssertionError
exception is
the last thing that the program will do. The program will come to halt
and will not continue. What if that is not what you want?
The try
and except
Block: Handling Exceptions
The try
and except
block in Python is used to catch and handle exceptions. Python executes code following the try
statement as a “normal” part of the program. The code that follows the except
statement is the program’s response to any exceptions in the preceding try
clause.As you saw earlier, when syntactically correct code runs into an
error, Python will throw an exception error. This exception error will
crash the program if it is unhandled. The except clause determines how your program responds to exceptions.The following function can help you understand the try and except block:def linux_interaction():
assert ('linux' in sys.platform), "Function can only run on Linux systems."
print('Doing something.')
linux_interaction() can only run on a Linux system. The assert in this function will throw an AssertionError exception if you call it on an operating system other then Linux.You can give the function a try using the following code:try:
linux_interaction()
except:
pass
|
output :
None
You got nothing. The good thing here is that the program did not
crash. But it would be nice to see if some type of exception occurred
whenever you ran your code. To this end, you can change the pass
into something that would generate an informative message, like so:try:
linux_interaction()
except:
print('Linux function was not executed')
Linux function was not executed
What you did not get to see was the type of error that was thrown as a result of the function call. In order to see exactly what went wrong, you would need to catch the error that the function threw.
The following code is an example where you capture the
AssertionError
and output that message to screen:
try:
linux_interaction()
except AssertionError as error:
print(error)
print('The linux_interaction() function was not executed')
OUTPUT :Function can only run on Linux systems.
The linux_interaction() function was not executed
AssertionError
, informing you
that the function can only be executed on a Linux machine. The second
message tells you which function was not executed.In the previous example, you called a function that you wrote yourself. When you executed the function, you caught the
AssertionError
exception and printed it to screen.Here’s another example where you open a file and use a built-in exception:
try:
with open('file.log') as file:
read_data = file.read()
except:
print('Could not open file.log')
OUTPUT :Could not open file.log
Exception FileNotFoundError
Raised when a file or directory is requested but doesn’t exist. Corresponds to errno ENOENT.
To catch this type of exception and print it to screen, you could use the following code:try:
with open('file.log') as file:
read_data = file.read()
except FileNotFoundError as fnf_error:
print(fnf_error)
OUTPUT :
[Errno 2] No such file or directory: 'file.log'
try
clause and anticipate catching various exceptions. A thing to note here is that the code in the try
clause will stop as soon as an exception is encountered.
Warning: Catching
Look at the following code. Here, you first call the Exception
hides all errors…even those which are completely unexpected. This is why you should avoid bare except
clauses in your Python programs. Instead, you’ll want to refer to specific exception classes you want to catch and handle. You can learn more about why this is a good idea in this tutorial.linux_interaction()
function and then try to open a file:try:
linux_interaction()
with open('file.log') as file:
read_data = file.read()
except FileNotFoundError as fnf_error:
print(fnf_error)
except AssertionError as error:
print(error)
print('Linux linux_interaction() function was not executed')
OUTPUT :
Function can only run on Linux systems.
Linux linux_interaction() function was not executed
try
clause, you ran into an exception immediately and did not get to the part where you attempt to open file.log. Now look at what happens when you run the code on a Linux machine:[Errno 2] No such file or directory: 'file.log'
Here are the key takeaways:- A
try
clause is executed up until the point where the first exception is encountered. - Inside the
except
clause, or the exception handler, you determine how the program responds to the exception. - You can anticipate multiple exceptions and differentiate how the program should respond to them.
- Avoid using bare
except
clauses.
The else
Clause
In Python, using the else
statement, you can instruct a program to execute a certain block of code only in the absence of exceptions.Look at the following example:
try:
linux_interaction()
except AssertionError as error:
print(error)
else:
print('Executing the else clause.')
OUTPUT :
Doing something.
Executing the else clause.
Because the program did not run into any exceptions, the
else
clause was executed.You can also
try
to run code inside the else
clause and catch possible exceptions there as well:try:
linux_interaction()
except AssertionError as error:
print(error)
else:
try:
with open('file.log') as file:
read_data = file.read()
except FileNotFoundError as fnf_error:
print(fnf_error)
OUTPUT :
Doing something.
[Errno 2] No such file or directory: 'file.log'
linux_interaction()
function ran. Because no exceptions were encountered, an attempt to open file.log was made. That file did not exist, and instead of opening the file, you caught the FileNotFoundError
exception.
Cleaning Up After Using finally
Imagine that you always had to implement some sort of action to clean
up after executing your code. Python enables you to do so using the finally
clause.Have a look at the following example:
try:
linux_interaction()
except AssertionError as error:
print(error)
else:
try:
with open('file.log') as file:
read_data = file.read()
except FileNotFoundError as fnf_error:
print(fnf_error)
finally:
print('Cleaning up, irrespective of any exceptions.')
finally
clause will be executed. It does not matter if you encounter an exception somewhere in the try
or else
clauses.OUTPUT :
Function can only run on Linux systems.
Cleaning up, irrespective of any exceptions.
#####################################################################################
thanks for visiting....
do love and share
visit @pythonLesson on Facebook
Python for Beginners
this Python book: The Complete Reference
by
Martin C. Brown
if you are the ADVANCE LEVEL than this book only for you
This comment has been removed by the author.
ReplyDelete