Part 14 - Exceptions
A mechanism designed to handle runtime errors or other problems (exceptions) inside a computer program.
Exceptions are very important, as they are raised whenever an error occurs in the system. (Or at least they should be.)
An exception stops the program if it is not caught.
Which stopped the program.
To handle the situation, exceptions must be caught.
Exceptions are either caught in a
try-except statement, a
try-ensure statement, or a
Exceptions are derived from the simple
This prevents the code from stopping and lets the program keep running even after it would have normally crashed.
There can be multiple
except statements, in case the code can cause multiple
Try-ensure is handy if you are dealing with open streams that need to be closed in case of an error.
As you can see, the
ensure statement didn't prevent the
Exception from bubbling up and causing the program to crash.
try-except-ensure combines the two.
If you don't solve the problem in your
There are times that you want to
Exceptions of your own.
Execute is called with an improper value of
i, then the
Exception will be
In production environments, you'll want to create your own
- Think of an exercise