Raising exceptions
Python's normal behavior is to execute statements in the order they are found, either in a file or at the >>>
prompt interactively. A few statements, specifically if
, while
, and for
, alter the simple top-to-bottom sequence of statement execution. Additionally, an exception can break the sequential flow of execution. Exceptions are raised, and this interrupts the sequential execution of statements.
In Python, the exception that's raised is also an object. There are many different exception classes available, and we can easily define more of our own. The one thing they all have in common is that they inherit from a built-in class called BaseException
.
When an exception is raised, everything that was supposed to happen is pre-empted. Instead, exception handling replaces normal processing. Make sense? Don't worry, it will!
The easiest way to cause an exception to occur is to do something silly. Chances are you&apos...