Python Try Catch Exceptions Tutorial

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-try-catch-exceptions-tutorial/
PINTEREST
PINTEREST
INSTAGRAM

As a developer we have a lot of thoughts about the applications and features. Implementing them tedious task. During the implementation we can make mistakes. Those mistakes can be related with applications logic or technical . These errors will cause problems while the application is running. We call this type of problems as exceptions. We can not prevent all exceptions but we can create some mechanisms to handle these situations. In this tutorial we will look how to handle these exceptions.

What is An Exception

Some of us may be familiar with exceptions but there may be others those do not have any idea about exceptions. Exceptions are unexpected error situations where applications generally stops working. Sometimes it will not stop working but will not work properly either.

Here is an example how an exception occurs. Our applications gets input from user and multiply given numbers. But there will be an exception if the user inputs string. File name is myapp.py .

When we call our application like below by providing a character like a other than a number we will get an exception.

What is An Exception

What is An Exception

We also get the exception type. Python provides different type of exceptions for different situations. In this code the exception type is TypeError .

Catch An Exception with Try and Except

Now we want to prevent this type of bugs and exceptions programmatically. Python provides try mechanism which is used to detect exceptions in given block of the code. We will put the potential error block into try like below. Here we guess that print(num1-num1) line can raise an exception.

LEARN MORE  Null Reference Exception

When we run by providing a character like a  it will not raise an exception. It simple skip to the except code block which will run if an exception is raised. Than application will flow in a normal situation.

Handle An Exception with Try and Except

Handle An Exception with Try and Except

Catching Specific Exceptions

In previous example we have fired single type of exception with except code block. But we can catch specific type of exception and ignoring others. As we remember from first example we get TypeError exception. If we only need to catch TypeError exception and ignore others we will specify TypeError to the except like below.

Catch Multiple Type Exceptions

We may need to catch multiple exceptions in a single except code block. We will provide the exception types in brackets by separating them with command (TypeA , TypeB , TypeC , ...)

In this example we will catch both TypeError and ZeroDivisionError .

Run Code After Exception with Finally

While checking exceptions in try code block we may need to execute some come whatever happens even try code block works or except code block works. We will use finally code block to complete the try-except blocks. After the try-except blocks finally code block will be run. This is generally used to release resources like file, database connection or revert operations used in try code block.

LEARN MORE  Dpkg Error "Dpkg Was Interrupted"

In this example we will close file handle in the finally code block whether previous codes blocks works or raise exception .

Raise Exception

Up to now we have handled self raised exceptions. We can also raise an exception manually without waiting a user input other any change. We will use raise keyword in order to raise an exception manually. In this example we will raise ZerrorDivisionError type exception.

Raise Exception

Raise Exception

User Defined Exceptions

While programming enterprise applications and libraries we will need to create our exception type library too. We may need to provide operational exceptions like invalid move operation for files. We can define our own user defined exceptions by using RuntimeError class like below. We will put following code into CorpExceptions.py

and we can raise as we did before after importing CorpExceptions.py

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/python-try-catch-exceptions-tutorial/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)