THE EXCEPTIONS IN PYTHON
Let’s start with an example regarding the exceptions in Python. The function divides the first parameter by the second and returns its value. If we pass a zero as the second parameter, we get an exception as Python is unable to divide by zero. It is said that an exception is raised by the Python runtime and the program crashes.
THE HIERARCHY OF CALLS
What occurs when an exception is raised is shown in the figure alongside. If the exception is raised in function f4(m, n) and is not handled, Python goes up the entire call hierarchy up to f1(a, b) and if this function does not handle it, Python terminates the execution of the program by printing a divide-by-zero error.
HIERARCHY OF EXCEPTIONS
Exceptions are objects, all exception objects provided by the language or created by us are instances of classes that ultimately derive from a single base class called BaseException. The figure alongside shows the hierarchy of exceptions that refers to a division by zero.
THE STATEMENT TRY EXCEPT
In the try block a suite of instructions must be put to check as they could throw an exception. If this happens, the execution of the code does not stop and does not start to go up the call stack but jumps to the except block where in all probability there will be a suite of instructions that will handle the error condition.
THE STATEMENT TRY EXCEPT AS
The as statement allows us to assign the exception object to a name so that it can be used in the instruction suite.
THE FINALLY CLAUSE
Obviously the slide is an example, one or more except blocks must always be inserted to handle the exception. finally is always executed regardless of whether or not you enter an except block. Its purpose is to free up resources, such as closing the connection to a database.
THE ELSE CLAUSE
else as finally is also optional and can be inserted after all except clauses. else is executed only if an exception is not raised. If present, the finally clause must necessarily be placed before finally which must be the last clause if present.
RAISE AND ASSERT
We can use raise to purposely raise an exception. Exception may not even be present although it rarely happens. This is the first mechanism by which raise is used, that is, to raise an exception.
At the first iteration, an IndexError exception is raised, then it goes back up the entire call hierarchy to see if there is a valid handler. If Python is not found, it prints the message shown in the figure and blocks the execution of the program.
We can also use the constructor of the IndexError class and in this case the passed text appears in the error message. The last case in which we use raise is when we catch an exception, but we want to re-launch it because maybe there is a handler somewhere else, for example a centralized error handler.
In this case, raise raises the ZeroDivisionError exception because it may be handled elsewhere.
THE ASSERT STATEMENT
With assert we tell Python to evaluate an expression to see if it is true or false, if false, an AssertionError exception is raised.
Leave A Comment