Exception Handling in Python.

Farida Aliyeva
4 min readNov 16, 2020

To start with, let us define what exactly an error in Python is. Overall, an error can be simply defined as a mistake in the code which might be harmful to the program or a device that is running it. Specifically, in Python, there are two types of errors: syntax error and exceptions.

Syntax errors are known to be the most common ones. They are also called parsing errors as they occur when the parser detects wrong statements or expressions. A basic example of syntax error:

Syntax Error.

The cause of a syntax error is shown in the error message to be extra parenthesis.

Exceptions happen regardless of syntactically correct statements or expressions. They happen during the execution of the program also called runtime errors. There are different types of exceptions whose types are printed right within the message. Example:

ZeroDivisionError Exception.

As you can see we dealt with syntax error and got an exception which is ZeroDivisionError.

TypeError Exception.

Another example of an exception — TypeError.

Now let us investigate how can we handle exceptions in Python. Handling exception is possible with the following methods:

· Try — which catches the exceptions raised by the program or Python itself.

· Raise — which allows triggering an exception manually.

Try-except block:

It is initiated with a try header which is in turn followed by a block of indented and finalizes with one or more optional except clauses. You can also add an optional else clause in the end. Pseudo-code:

Try-except Pseudo-code.

Try block is executed first, then

If Python exception does not occur, except block is skipped and we move on to else block.

If Python exception occurs while try block is executing, the remaining execution stops, and Python searches for a matching exception handling block.

Matching block found — then execute that block, after the exception is handled execute the rest of the try block.

Matching block not found — then stop the execution with the error message.

Note that in Python it is possible can handle as many exceptions as you wish.

Remember that if you don’t specify an exception type on the except line, it will catch all exceptions, which is a bad idea, since it means your program will ignore unexpected errors as well as ones which the except block is actually prepared to handle.

Try-finally clause

try except finally

Another possible form is Try — finally clause. When we use finally clause in a try block, its block of statements then its block will always run no matter if an exception occurred within try block or not:

· In case no exception occurred, Python will run the try block, finally block, and then continues executing the rest.

· In case an exception occurred, Python executes the finally block and then propagates execution to except block to handle the exception.

Example with finally.

As you can see both except and finally clauses are executed.

Make sure you specify an exception type on the except line, another way it will catch all exceptions, which is a bad programming approach, since your program will ignore any unexpected errors as well as ones which the except block is actually prepared to handle.

Raising Exceptions

Raise statement enables the programmer to force a specific exception to occur. So, if you want to throw an error when a certain condition occurs use raise can assist you.

Example code:

Raising exception example.

These are the basics must-know things to know about Exception Handling in Python Programming Language.

Thank you for your attention! :)

--

--

Farida Aliyeva

Data Scientist at SDH | MS Graduate in Computer Science and Data Analytics