How to Handle Exceptions in Python

It is very common to encounter errors during the execution of a program. Two common kinds of errors that you may have to deal with are syntax errors and exceptions. Syntax errors occur when you type the code incorrectly. In such cases, the erroneous line is repeated by the parser with an arrow pointing to the earliest location where the error was detected.

Exceptions are different from syntax errors. They occur during the execution of a program when something unexpected happens. For example, let’s say you are asking the user to input a number in order to perform a division. Now, if the user enters a string instead of a number and you try to divide a number by the given input, the program will output a TypeError.

When you are not properly handling exceptions, the program will abruptly quit as it won’t know what to do in such cases. The following code is such an example:

As long as you are entering an integral input value, the program will work correctly. However, as soon as you enter a string or even a decimal number as input, you will get a ValueError exception.

From the traceback above, we can see that the user inputs a string n instead of a number leading to the ValueError. The traceback also shows us the exact location of the error, i.e line 4 allowing us to go back to the code and fix it.

In this tutorial, you will learn how to properly handle and raise exceptions in Python.

Some Common Exceptions

Here are some basic exceptions that you might encounter when writing programs. You can read about many more built-in exceptions on the official website.

  • NameError: This exception is raised when the program cannot find a local or global name. The name that could not be found is included in the error message.
  • TypeError: This exception is raised when a function is passed an object of the inappropriate type as its argument. More details about the wrong type are provided in the error message.
  • ValueError: This exception occurs when a function argument has the right type but an inappropriate value.
  • NotImplementedError: This exception is raised when an object is supposed to support an operation but it has not been implemented yet. You should not use this error when the given function is not meant to support the type of input argument. In those situations, raising a TypeError exception is more appropriate.
  • ZeroDivisionError: This exception is raised when you provide the second argument for a division or modulo operation as zero.
  • FileNotFoundError: This exception is raised when the file or directory that the program requested does not exist.

Just like the names above, most exceptions have self-explanatory names.

Handling an Exception

The code at the beginning of the article asked users to enter an integer as input. If the user did not provide an integer input, the program stopped execution and raised a value error exception. In this section, we will write some code to tell the user that their input is not a valid integer value.

Handling exceptions allows our programs to continue executing even when errors occur.

Try-Except Clause

The first step of the process is to include the code that you think might raise an exception inside the try clause. The next step is to use the except keyword to handle the exception that occurred in the above code. The modified code for the user input will look like this:

What happens here is that the program tries to execute the code inside the try clause. If no exception was raised, the program skips the except clause and the rest of the code executes normally. If an exception is raised, the program skips the remaining code inside the try clause and the type of the exception is matched with the name of the exception after the except keyword. In case of a match, the code inside the except clause is executed first, and then the rest of the code after the try clause is executed normally.

When you enter an integer as an input, the program gives you the final result of the division. When a non-integral value is provided, the program prints a message asking you to try and enter an integer again. Note that this time, the program does not abruptly quit when you provide some invalid input.

If you enter a number which is not an integer , you will get the following output on your terminal.

You can have multiple except clauses to handle different exceptions. Please keep in mind that these handlers will only deal with exceptions that occurred in the corresponding try clause. They will not handle any exceptions raised within other exception handlers.

You can also handle multiple exceptions using a single except clause by passing these exceptions to the clause as a tuple.

Finally, you can also leave out the name of the exception after the except keyword. This is generally not recommended as the code will now be catching all the exceptions and handling them in the same way. This is not optimal as you will be handling a TypeError exception the same way as you would have handled a ZeroDivisionError exception. When handling exceptions, it is better to be as specific as possible and only catch what you can handle.

One possible use of catching all exceptions is to properly print out the exception error on screen like the following code:

Using the Else Clause

You can also use an else clause in a try ... except statement. The else clause is meant to contain code that needs to be executed if the try clause did not raise any exceptions. This can be useful to make sure that you don’t add any code to the try block whose exceptions you don’t intend to catch. One thing worth mentioning is that if you decide to use an else clause, you should include it after all the except clauses but before the finally block.

In our case, we could move the line that prints the result of our division inside the else block.

Cleaning Up Using the Finally Clause

Let’s say you have written some code inside the try block that is supposed to perform a task by utilizing a large amount of resources. It is important to release those resources back when you are done using them. This can be easily achieved by using the finally clause.

The code inside the finally clause is always executed irrespective of whether the try block raised an exception. You can verify this by running the following code:

If any of the except clauses that you specified do not handle the raised exception, the same exception is raised again after the execution of code inside the finally block.

A More Complex Example

In this section, we will write a program to deal with multiple exceptions. Just like the previous examples, we will be performing some mathematical operations. However, this time we will take the input from a list.

The following code checks for two exceptions, TypeError and ValueError. The else block is used to print the factorial. You can see in the output that this code is executed only when no exception is raised.

The above code produces the following output:

Another thing worth noticing is that the code inside the finally clause is executed for each item in the list.

User-Defined Exceptions

Sometimes, you want to raise exceptions which are not provided by Python.  User-defined exception give programs more readability. For example, suppose you were designing a part of a program that checks if coupon codes submitted by users are valid, then you would need to create a custom Exception for that purpose.

Custom exceptions inherit from the python base Exception class as follows.

The exceptions are also defined similar to the Python exceptions by ending with Error, though this is optional.

Once the Exception is defined,  you can use it in your program as follows.

In the code above, we ask the user for a coupon code, and if the coupon code they provide is not in the coupon_list, a CouponError is raised.

To customize the error, even more, you can define a more friendly message to the user.

Now if you run the program again, the user gets a more friendly error message

Final Thoughts

I hope this tutorial helped you understand exception handling in Python. Additionally, don’t hesitate to see what we have available for sale and for study in the marketplace, and don’t hesitate to ask any questions and provide your valuable feedback using the feed below.

Properly handling exceptions can be very helpful in situations where exiting a program after it receives an unexpected input is not viable. If you have any questions related to exception handling in Python, please let me know in the comments.

Learn Python

Learn Python with our complete Python tutorial guide, whether you’re just getting started or you’re a seasoned coder looking to learn new skills.

This post has been updated with contributions from Esther Vaati. Esther is a software developer and writer for Envato Tuts+.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen

It is very common to encounter errors during the execution of a program. Two common kinds of errors that you may have to deal with are syntax errors and exceptions. Syntax errors occur when you type the code incorrectly. In such cases, the erroneous line is repeated by the parser with an arrow pointing to the earliest location where the error was detected.

Exceptions are different from syntax errors. They occur during the execution of a program when something unexpected happens. For example, let's say you are asking the user to input a number in order to perform a division. Now, if the user enters a string instead of a number and you try to divide a number by the given input, the program will output a TypeError.

When you are not properly handling exceptions, the program will abruptly quit as it won't know what to do in such cases. The following code is such an example:

As long as you are entering an integral input value, the program will work correctly. However, as soon as you enter a string or even a decimal number as input, you will get a ValueError exception.

From the traceback above, we can see that the user inputs a string n instead of a number leading to the ValueError. The traceback also shows us the exact location of the error, i.e line 4 allowing us to go back to the code and fix it.

In this tutorial, you will learn how to properly handle and raise exceptions in Python.

Some Common Exceptions

Here are some basic exceptions that you might encounter when writing programs. You can read about many more built-in exceptions on the official website.

  • NameError: This exception is raised when the program cannot find a local or global name. The name that could not be found is included in the error message.
  • TypeError: This exception is raised when a function is passed an object of the inappropriate type as its argument. More details about the wrong type are provided in the error message.
  • ValueError: This exception occurs when a function argument has the right type but an inappropriate value.
  • NotImplementedError: This exception is raised when an object is supposed to support an operation but it has not been implemented yet. You should not use this error when the given function is not meant to support the type of input argument. In those situations, raising a TypeError exception is more appropriate.
  • ZeroDivisionError: This exception is raised when you provide the second argument for a division or modulo operation as zero.
  • FileNotFoundError: This exception is raised when the file or directory that the program requested does not exist.

Just like the names above, most exceptions have self-explanatory names.

Handling an Exception

The code at the beginning of the article asked users to enter an integer as input. If the user did not provide an integer input, the program stopped execution and raised a value error exception. In this section, we will write some code to tell the user that their input is not a valid integer value.

Handling exceptions allows our programs to continue executing even when errors occur.

Try-Except Clause

The first step of the process is to include the code that you think might raise an exception inside the try clause. The next step is to use the except keyword to handle the exception that occurred in the above code. The modified code for the user input will look like this:

What happens here is that the program tries to execute the code inside the try clause. If no exception was raised, the program skips the except clause and the rest of the code executes normally. If an exception is raised, the program skips the remaining code inside the try clause and the type of the exception is matched with the name of the exception after the except keyword. In case of a match, the code inside the except clause is executed first, and then the rest of the code after the try clause is executed normally.

When you enter an integer as an input, the program gives you the final result of the division. When a non-integral value is provided, the program prints a message asking you to try and enter an integer again. Note that this time, the program does not abruptly quit when you provide some invalid input.

If you enter a number which is not an integer , you will get the following output on your terminal.

You can have multiple except clauses to handle different exceptions. Please keep in mind that these handlers will only deal with exceptions that occurred in the corresponding try clause. They will not handle any exceptions raised within other exception handlers.

You can also handle multiple exceptions using a single except clause by passing these exceptions to the clause as a tuple.

Finally, you can also leave out the name of the exception after the except keyword. This is generally not recommended as the code will now be catching all the exceptions and handling them in the same way. This is not optimal as you will be handling a TypeError exception the same way as you would have handled a ZeroDivisionError exception. When handling exceptions, it is better to be as specific as possible and only catch what you can handle.

One possible use of catching all exceptions is to properly print out the exception error on screen like the following code:

Using the Else Clause

You can also use an else clause in a try ... except statement. The else clause is meant to contain code that needs to be executed if the try clause did not raise any exceptions. This can be useful to make sure that you don't add any code to the try block whose exceptions you don't intend to catch. One thing worth mentioning is that if you decide to use an else clause, you should include it after all the except clauses but before the finally block.

In our case, we could move the line that prints the result of our division inside the else block.

Cleaning Up Using the Finally Clause

Let's say you have written some code inside the try block that is supposed to perform a task by utilizing a large amount of resources. It is important to release those resources back when you are done using them. This can be easily achieved by using the finally clause.

The code inside the finally clause is always executed irrespective of whether the try block raised an exception. You can verify this by running the following code:

If any of the except clauses that you specified do not handle the raised exception, the same exception is raised again after the execution of code inside the finally block.

A More Complex Example

In this section, we will write a program to deal with multiple exceptions. Just like the previous examples, we will be performing some mathematical operations. However, this time we will take the input from a list.

The following code checks for two exceptions, TypeError and ValueError. The else block is used to print the factorial. You can see in the output that this code is executed only when no exception is raised.

The above code produces the following output:

Another thing worth noticing is that the code inside the finally clause is executed for each item in the list.

User-Defined Exceptions

Sometimes, you want to raise exceptions which are not provided by Python.  User-defined exception give programs more readability. For example, suppose you were designing a part of a program that checks if coupon codes submitted by users are valid, then you would need to create a custom Exception for that purpose.

Custom exceptions inherit from the python base Exception class as follows.

The exceptions are also defined similar to the Python exceptions by ending with Error, though this is optional.

Once the Exception is defined,  you can use it in your program as follows.

In the code above, we ask the user for a coupon code, and if the coupon code they provide is not in the coupon_list, a CouponError is raised.

To customize the error, even more, you can define a more friendly message to the user.

Now if you run the program again, the user gets a more friendly error message

Final Thoughts

I hope this tutorial helped you understand exception handling in Python. Additionally, don’t hesitate to see what we have available for sale and for study in the marketplace, and don't hesitate to ask any questions and provide your valuable feedback using the feed below.

Properly handling exceptions can be very helpful in situations where exiting a program after it receives an unexpected input is not viable. If you have any questions related to exception handling in Python, please let me know in the comments.

Learn Python

Learn Python with our complete Python tutorial guide, whether you're just getting started or you're a seasoned coder looking to learn new skills.

This post has been updated with contributions from Esther Vaati. Esther is a software developer and writer for Envato Tuts+.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen


Print Share Comment Cite Upload Translate Updates
APA

Monty Shokeen | Sciencx (2017-04-13T18:34:37+00:00) How to Handle Exceptions in Python. Retrieved from https://www.scien.cx/2017/04/13/how-to-handle-exceptions-in-python/

MLA
" » How to Handle Exceptions in Python." Monty Shokeen | Sciencx - Thursday April 13, 2017, https://www.scien.cx/2017/04/13/how-to-handle-exceptions-in-python/
HARVARD
Monty Shokeen | Sciencx Thursday April 13, 2017 » How to Handle Exceptions in Python., viewed ,<https://www.scien.cx/2017/04/13/how-to-handle-exceptions-in-python/>
VANCOUVER
Monty Shokeen | Sciencx - » How to Handle Exceptions in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/04/13/how-to-handle-exceptions-in-python/
CHICAGO
" » How to Handle Exceptions in Python." Monty Shokeen | Sciencx - Accessed . https://www.scien.cx/2017/04/13/how-to-handle-exceptions-in-python/
IEEE
" » How to Handle Exceptions in Python." Monty Shokeen | Sciencx [Online]. Available: https://www.scien.cx/2017/04/13/how-to-handle-exceptions-in-python/. [Accessed: ]
rf:citation
» How to Handle Exceptions in Python | Monty Shokeen | Sciencx | https://www.scien.cx/2017/04/13/how-to-handle-exceptions-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.