A try...except...else...finally statement can have more than one except. Also, each except can have one or more statements that address specific errors. For example, instead of displaying the exception information, you can display a message that explains how to solve the problem.
- Change the name of your script from exceptions_division_calc_try_this to exceptions_division_calc_your_turn.
- Replace this block:
except Exception as e: print("Exception = ", e) et = type(e) print("Exception type = ", et)
...with this:
except ZeroDivisionError: print("Can't divide by zero.") except TypeError: print("Expected a number.")
- Click Save to save a copy of your script.
- Run the script with d = 5.
- Verify that the script provides a quotient result when when d is set to a non-zero integer, in this case, 5:
Now let's repeatt the tests three times, changing the value of d each time:
- Then test dividing by zero with d = 0.
- Verify that the script displays "Can’t divide by zero." when d is set to 0.
- Finally, test it one more time, using d = "Hello".
- Verify that the script displays "Expected a number."