Types of Exception in Python

An exception is an event, which occurs during the execution of a program and disrupts the normal flow of the program’s instructions. When a program encounters a situation that it can’t deal with, it raises an exception. Therefore, we say that an exception is an object that represents an error.

Exception Handling in Python:

We can handle exceptions by using try block and except block. A critical operation that can raise an exception is placed inside the try block and the code handles exception is written in except block. It has the following syntax:

except Exception-name:

The try statement works as follows:
Step1: First, the try block is executed.
Step2: If no exception occurs, the except block is skipped.
Step3: If an exception occurs, during the execution of any statement in the try block and then,

i. The rest of the statements in the try block are skipped.
ii. If the exception type matches the exception named after the except keyword, the except block is executed and then execution continues after the try statement.
iii. If an exception occurs that does not match the exception named in the except block, then it is passed on to the outer try block. If no exception handler is found in the program, then it is an unhandled exception and the program is terminated with an error message.

Multiple Except for Blocks:

Python allows you to have multiple except blocks for a single try block. The block which matches with the exception generated will get executed. A try block can be associated with more than one except block to specify handlers for different exceptions. However, only one handler will be executed. Exception handlers only handle exceptions that occur in the corresponding try block. We can write our programs that handle selected exceptions. It has the following syntax:

operations are done in this block
except Exceptional:
If there is Exceptional, then execute this block
except Exceptiona2:
If there is Exceptiona2, then execute this block
If there is no exception then execute this block

Multiple Exceptions in a Single Block:

An except clause may name multiple exceptions as a parenthesized tuple, So, whatever exception is raised, out of the three exceptions are specified, the same except block will be executed.

Except Block without Exception:

You can even specify an except block without mentioning any exception. This type of except block if present should be the last one that can serve as a wildcard. But use it with extreme caution, since it may mask a real programmer error.

In large software programs, many times, it is difficult to anticipate all types of possible exceptional conditions. Therefore, the programmer may not be able to write a different handler for every individual type of exception. In such situations, a better idea is to write a handler that would catch all types of exceptions. It has the following syntax:

The else Cluse:

The try… except block can optionally have an else clause, which, when present must follow all except blocks. The statements in the else block are executed only if the try clause does not raise an exception.

Raising Exceptions:

You can deliberately raise an exception using the raise keyword. The general syntax for the raise statement is:
raise [Exception [, args [, traceback]]]

Here, Exception is the name of the exception to be raised, and args is optional and specifies a value for the exception argument. If args isn’t specified, then the exception argument is None. In the final argument, the traceback is also optional and if present is the traceback object used for the exception.

Re-Raising Exceptions:

Python allows programmers to re-raise an exception. For example, an exception thrown from the try block can be handled as well as re-raised in the except block using the keyword raise.

Instantiating Exceptions:

Python allows programmers to instantiate an exception first before raising it and adding any attributes to it as desired. These attributes can be used to give additional information about the error. To instantiate the exception, the except block may specify a variable after the exception name. The variable then becomes an exception instance with the arguments stored in the [python]instance. args[/python]. The exception instance also has the [python]__str__()[/python] method defined so that the arguments can be printed directly without using [python]instance.args[/python].