How we raise exception and handle errors in python is called the exception mechanism, which is declaring and responding to exceptional conditions.
Exceptions can be handled by program, preventing the interpret from halting, which unhandled will do cause python to halt.
Tips:
- Exceptions are objects. They have classes with constructor.
- They enable non-local continuations of control.
- Exception handling tends to be slow, so should not be used in standard situations.
Raising Exception
assertstatement raise an exception of typeAssertionError- Can be ignored by running with “-O” flag.
raisestatement:raise <exception><exception>must evaluate to a subclass ofBaseExceptionor an instance of one.
Exceptions are constructed like any other object.
E.g., TypeError('Bad argument!')
TypeError— A function was passed the wrong number/type of argumentNameError— A name wasn’t foundKeyError— A key wasn’t found in a dictionaryRuntimeError— Catch-all for troubles during interpretation
try Statement - Handle Exceptions
try:
<try suite>
except <exception class> as <name>:
<except suite>
finally: # optional
<finally suite>Execution rule:
- The
<try suite>is executed first. - If, during the course of executing the
<try suite>, an exception is raised that is not handled otherwise, and - If the class of the exception inherits from
<exception class>, then - The
<except suite>is executed, with<name>bound to the exception. - The
<finally suite>will be executed no matter whether an error has been handled.
try:
x = 1 / 0
except ZeroDivisionError as e:
print(f"handlina a {type(e)}")
x = 0Multiple try statement:
Control jumps to the except suite of the most recent try statement that handles that type of exception.
The exception will jump out until being handled by one of except suite and then it is handled, no error anymore.