1 17-Exceptions


Next: 16-TypeFormatDebug.html

**There are no exceptions to the rule, that everybody likes to be an exception to the rule.**
    **- Charles Osgood**

1.1 Screencasts

1.2 Extra reading

1.3 Introduction

1.3.1 Abstract problem

1.3.2 Practical solution

Exception handling
* is a process of handling exceptional situations that may occur in a program.
* allows programmers to cleanly separate the code that implements the focused algorithm, from the code that deals with exceptional situations that the algorithm may face rarely.
* Upon encountering an exception, the compiler/interpreter can:
* redirect program flow to an appropriate error-handling code segment, or
* terminate gracefully; i.e., it will give a proper message and then will terminate the program.
* Exception handling communicates the existence of a run-time problem or error, redirecting program flow from where it was detected, to where the issue can be handled.
* Un-handled exceptions stop program execution, but you can choose to manually handle exceptions to continue running.
* When an exception is created, it is also commonly called raising or throwing an exception.

1.3.3 Keywords

Keywords in python:
assert, try, raise, catch, finally

1.3.3.1 Simple assertions

`assert condition_that_should_be_true, "comment about error if it's not"`

When condition_that_should_be_true is False, i.e., not True, comment about error if it's not is printed, and the program is terminated.

1.3.3.2 Full exception handling

try:
    statements
    raise <obj/class> # optional, with optional obj itself
except:
    statements
else: # optional
    statements
finally: # optional
    statements

1.3.4 Benefits

1.4 Examples

Check out the code

1.5 Stack unwinding

Trace the execution: code after exception, until except block, is skipped

1.6 Some Python3 standard exceptions

17-Exceptions/exception_hierarchy.png
The above image is an older subset, and the full tree of python3’s built-in exceptions is:
BaseException
+– SystemExit
+– KeyboardInterrupt
+– GeneratorExit
+– Exception
+– StopIteration
+– StopAsyncIteration
+– ArithmeticError
| +– FloatingPointError
| +– OverflowError
| +– ZeroDivisionError
+– AssertionError
+– AttributeError
+– BufferError
+– EOFError
+– ImportError
| +– ModuleNotFoundError
+– LookupError
| +– IndexError
| +– KeyError
+– MemoryError
+– NameError
| +– UnboundLocalError
+– OSError
| +– BlockingIOError
| +– ChildProcessError
| +– ConnectionError
| | +– BrokenPipeError
| | +– ConnectionAbortedError
| | +– ConnectionRefusedError
| | +– ConnectionResetError
| +– FileExistsError
| +– FileNotFoundError
| +– InterruptedError
| +– IsADirectoryError
| +– NotADirectoryError
| +– PermissionError
| +– ProcessLookupError
| +– TimeoutError
+– ReferenceError
+– RuntimeError
| +– NotImplementedError
| +– RecursionError
+– SyntaxError
| +– IndentationError
| +– TabError
+– SystemError
+– TypeError
+– ValueError
| +– UnicodeError
| +– UnicodeDecodeError
| +– UnicodeEncodeError
| +– UnicodeTranslateError
+– Warning
+– DeprecationWarning
+– PendingDeprecationWarning
+– RuntimeWarning
+– SyntaxWarning
+– UserWarning
+– FutureWarning
+– ImportWarning
+– UnicodeWarning
+– BytesWarning
+– ResourceWarning

See code and above links for more detail.

1.7 Language focus

Code (to be stepped through in spyder, since pudb won’t launch with errors in the file):
17-Exceptions/exceptions_00_overview.py

Next: 18-InputOutput.html