Python Error Handling
Raise an Exception:
- By raising an exception, you can cause the code to crash and indicate the error if an unacceptable condition is met
Asserts:
- An assert is a quick way to check if a condition is met, otherwise it will raise an AssertionError exception with the specified error
- It is optional to specify the error text, but you must not have a comma if you don't include the text
Handle Exceptions:
- If any code in the 'try' area fails with an Exception, the program will execute the code in the 'except' area
- Some functions from built-in or external Python libraries can throw exceptions, so you may want to hande them
Handle and Display Exception Error
- The description of the exception will be stored in the variable 'e' which can be displayed
Responding to Known Exception Types
- You can check for different error types and respond differently to each one
- You can find a list of exception types online
Printing stack trace (path of function calls to error)
- This shows the error and path to it as it would if the exception had not been handled, but you can also add code to handle it
Challenge
Write a function that will divide two numbers and have it display a warning message and return 0 if a ZeroDivisionError exception is found. If any other exception is found, have it print "Something went wrong" and display the error stack trace.