CSE 1010 Lecture 20: EXCEPTIONS CONTINUED
WEEK 12 - LECTURE 20 - EXCEPTIONS CONTINUED
Exceptions
● See a program
○ Outcome
○ Is there any problem?
■ Yes! Recall last note.
■ b cannot be zero
○ So there is an exception in this program
■ See the last line: ZeroDivisionError
■ We cannot divide a number by zero
○ We can let Python raise the ZeroDivisionError exception, or we can raise our
own exception
■ Where to add?
● Above c = a / b
■ Add what?
● If b == 0:
Raise Exception(‘Refusing to divide by zero’)
jadetortoise401 and 55 others unlocked
84
CSE 1010 Full Course Notes
Verified Note
84 documents
Document Summary
Week 12 - lecture 20 - exceptions continued. So there is an exception in this program. We cannot divide a number by zero. We can let python raise the zerodivisionerror exception, or we can raise our own exception. This is the flip side of catching an exception. Here we are raising our own exception instead of letting python raise the zerodivisionerror exception. If b == 0 then execution never gets to this point. Raise exception( refusing to divide by zero") is what we type in the program; this is the statement that generated the exception. Last line is the exception report: what we write in the parenthesis. Raising (or throwing) an exception is a way to exit a function when you are unable to return a valid result. Catching (or handling or intercepting) an exception is a way to detect when an exception was raised. You can raise your own custom exceptions. You can catch standard or custom exceptions.