Python Lessons - 4.1 Exceptions m

Python Lessons – 4.1 Exceptions

These exceptions happen when something goes wrong in your code by generating an error. The reasons why these errors are generated can be many, for example due to a resource not found for example a file, or to a value with an incompatible format for a particular operation, etc.

Python Lessons - 4.2 Exception Handling m

Python Lessons – 4.2 Exception Handling

When code execution encounters an error, it throws an exception that stops the program from running. To avoid this you can handle the exception, in order to correct the error, have the possibility to remedy or provide an output information on the nature of the error occurred so then eventually understand the nature and correct the code.

Python Lessons - 4.3 Finally

Python Lessons – 4.3 Finally

In Python There is an additional clause to add to the try-except construct called finally. This clause also defines a specific block of code that will be executed in all cases. That is, the finally block is executed both if the try block is terminated without exception, or if any exception occurred (both managed and unmanaged).

Python Lessons - 4.4 Raise an exception

Python Lessons – 4.4 Raise an exception

It can happen sometimes that we ourselves want to create and throw an exception. The reasons for doing this can be many. Often you might want to create a more specific exception, which signals a specific error to those who are using our code in the future.

Python lessons - 4.5 Asserts m

Python Lessons – 4.5 Asserts

Assertions (assert) are a very useful control tool to use when writing a code in the test (DEBUG) phase. Each assertion introduced in the code performs a check at a precise time of execution, using a condition. If the condition is satisfied then the execution goes forward, otherwise a Asserterror exception is raised that blocks the execution of the program.

Python Lessons - 4.9 Handling files

Python Lessons – 4.9 Handling files

In the previous sections we have seen how to read and write the data within a file, but there are some precautions to follow. We have seen that it is important to always close the file we are working on while running the program.