CSE 1010 Lecture 21: CSE 1010 - Lecture 21 - Exceptions
CSE 1010 verified notes
21/29View all
Document Summary
A function should return a correct value or it should not return at all. This does not mean that a function should intentionally get stuck in an unbounded loop (thus, not returning) There should be some other means of exiting a function, something that is outside the normal path of execution. Example: def divide(x, y): if y != 0: return x / y. A correct value can be returned, otherwise, this function can"t return a correct value, so it shouldn"t return at all. An exception is a way to indicate that something unexpected happened, or something out of the ordinary. An exception is pretty much a fancy way to say that there"s an error. A function that raises an exception exits, but it does not return in the normal way you"re used to. This one is because there"s no colon after the parentheses. This one is because 2 is the largest index, not 3.