Python exceptions

Python exceptions

If an operation cannot be completed due to an error, the program must:

  • return to a stable state and allow other operations

  • try to save your work and finish

This task is difficult because usually the code that detects the error is not the one that can perform such tasks this is why you should inform that you can handle it

The most common solution are the error codes

Python offers another way to deal with errors under exceptional conditions: the exceptions

An exceptional condition is one that prevents the continuation of an operation

It's not known how to handle it, but you can't continue

In Python, an exception is thrown for someone who knows how to handle it to handle it in a higher context

In the example the open function checks whether the file readme.txt existed in the system and if it doesn't exist the exception is thrown FileNotFoundError who is untreated at the moment

When the interpreter detects the throw of the exception, the current method ends and launches an object that provides information about the error that occurred

Normally each type of error will be handled individually

Handling of the exceptions

Once detected the error makes it necessary to indicate who is in charge of treating it

An exception handler has the following format:

try block

The block try delimits the group of operations that may produce exceptions

except block

The block except it is the place to which control is passed if any of the operations throws an exception

The example used a block try with his block except to handle the errors in the previous example

If any of the operations in the block throw an exception, the block is interrupted try and runs the except

At the end of this, it is normally continued

If no exception is thrown, the block except is ignored

Multiple exceptions

There is also the possibility to use an exception name to handle specific errors

Some of which are already recognized by the system, such as the exception FileNotFoundError, which is launched when a file does not exist in the system

The example used a block try with several blocks except to handle several exceptions

A block try can have several except associated

We've added the sentence as to the block except after naming it to make it the object of exception we've called and

As you can see, after turning it into an object, we can use it as if it were a class

When an exception does not correspond to any catch spreads backwards in the sequence of invocations until a catch adequate

In the example is managed the exceptions of the methods f1 and f2 and if you don't find any, it continues the execution of the code in the normal way outside of the block try

The sentence has been used raise followed by the name of the type of exception to propagate it and treat it elsewhere

In this case we have treated it within the f1 function itself in its exception blocks

Although spreading it could have been treated outside of it elsewhere in the script

A block has also been used else that allows the code to continue execution in case there are no exceptions before continuing outside the block try

else block

Allows code execution to continue in case there are no exceptions before continuing outside the block try

The block else can be used to exit a loop if the statement is added at the end of a loop break

It is optional and if it is not used, it will continue the execution of the code in the block finally and finally outside the block try

Finally block

There may be occasions in which you want to perform some operation if exceptions occur as if not

Such operations can be placed within a block finally

If there is no exception, the block will be executed try, then the block else and finally the finally

Like the block else, is optional and if it is not used, it will continue to execute the code outside the block try

Rules of use

  1. Standard

    • If an exception can be handle should not be spread

    • It is more convenient to use methods that do not produce errors

  2. Standard

    • Do not use exceptions to avoid a query

      • Not to abuse them

  3. Standard

    • To separate the error handling logic

      All together

      Separate

  4. Standard

    • Do not ignore an exception already that we leave the code with errors without control