Errors And Exceptions
- Dividing something by zero, for example
1/0will provide the outputdivision by zero. - Errors and Exceptions are the same thing.
- All of these errors and exceptions extend a class called the
base exception.- For example the
division by zeroerror extends the arithmetic error, which extends the exception, which extends the base exception. Thebaseexceptions gives us useful and powerful properties of exceptions.- It halts the execution of the code and provides you reasons on why it was halted.
- For example the
- All of these errors and exceptions extend a class called the
- In an error, the error will point to the line number with an arrow, for example:
----> 1 1/0.- The entire error output that you see is called a
stack trace.- These are nested operations and provide ways to debug the program.
- The entire error output that you see is called a
- Useful stack trace example: ``` def causeError(): return 1/0
def callCauseError(): return causeError()
callCauseError()
* A useful way to catch an exception is via a `try/except` statement.
try: 1/0
We are catching the exception here
except Exception as e: print(type(e))
causeError()
* This will output:
<class ‘ZeroDivisionError’> ```
- We see from the above that the exception has been caught and is not being raised anymore.
- It is just a class, has attributes and can even be returned.
- Exceptions when used correctly are like a secondary layer of code.