CSE 1010 Lecture 20: EXCEPTIONS CONTINUED

48 views2 pages
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’)
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in
jadetortoise401 and 55 others unlocked
CSE 1010 Full Course Notes
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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents