Exceptions -- raising and catching
- Define an exception type SpecialExp.
- Implement the following functions:
- inner2() -- raise an exception of type SpecialExp.
- inner1() -- call inner2.
- outer -- call inner1. Wrap this call in a try:except:
statement that catches exception SpecialExp and only that
specific exception.
- Run your script.
- In inner1 or inner2, generate another exception, for
example, by referencing an undefined variable.
- Run your script again. Make sure that only SpecialExp
is caught.
- Add another clause to your try:except: statement -- Catch
the additional exception.
What you will learn:
- How to define an exception type.
- How to raise an exception of a specific type.
- How to catch an exception of a specific type (and no other).