Python Try Catch Exceptions Tutorial

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

As a developer, we have a lot of thoughts about the applications and features. Implementing them a tedious task. During the implementation, we can make mistakes. Those mistakes can be related to 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 at 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 stop working. Sometimes it will not stop working but will not work properly either.

Here is an example of how an exception occurs. Our applications get input from the user and multiply given numbers. But there will be an exception if the user inputs string. The filename is myapp.py .

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

What is An Exception
What is An Exception

We also get the exception type. Python provides a 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 a mechanismtry which is used to detect exceptions in the given block of the code. We will put the potential error block into liketry below. Here we guess that lineprint(num1-num1) can raise an exception.

LEARN MORE  C# Programming Language Null Reference Exception and Solution

When we run by providing a character like ita will not raise an exception. It simple skip to the codeexcept block which will run if an exception is raised. Then 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 the previous example, we have fired a single type of exception with the codeexcept block. But we can catch a specific type of exception and ignoring others. As we remember from the first example we get the exceptionTypeError. If we only need to catch an exceptionTypeError and ignore others we will specify toTypeError the likeexcept below.

Catching Specific Exceptions
Catching Specific Exceptions

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  Apache Log Files

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 for a user input other any change. We will use keywordraise in order to raise an exception manually. In this example, we will raise typeZerrorDivisionError 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 classRuntimeError like below. We will put the following code into CorpExceptions.py

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

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM