{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

10+-+Exceptions - EECS 280 Programming and Introductory...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Exceptions EECS 280 Programming and Introductory Data Structures 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Exceptions Motivation We want a means of recognizing and handling unusual conditions in your program at runtime, not just at compile time. If you have a function that does not produce meaningful results for all possible values of its inputs, then it is a partial functions . We have seen one particular way of preventing a partial function from receiving 2
Background image of page 2
Exceptions Motivation Instead of the REQUIRES clause, let’s look at another way of ensuring correct inputs: runtime checking . So, if we can’t guarantee formally (via a specification) that the inputs are correct, maybe we can guarantee this by checking the inputs explicitly before using them in our program. One nice things about REQUIRES, is that we don’t have to figure out what constitutes 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Exceptions Determining legitimate output for illegitimate input There are three general strategies for determining legitimate output for illegitimate input: 1. “It’s my problem!” Try to “fix” things and continue execution by “coercing” legitimate inputs from illegitimate ones by either modifying them or returning default outputs that make sense in the context in which the function is defined. For example, list_rest() could return an 4
Background image of page 4
Exceptions Determining legitimate output for illegitimate input There are three general strategies for determining legitimate output for illegitimate input: 1. “It’s my problem!” However, this strategy fails whenever the function cannot implement its specification with the given inputs. For example, what is factorial to do if it is passed a negative integer? Factorial is simply undefined for negative 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Exceptions Determining legitimate output for illegitimate input There are three general strategies for determining legitimate output for illegitimate input: 2. “I Give up!” Use something like assert() or exit() . assert(condition) is a c-library function that says: “terminate the program if condition is not true.” It is useful for checking invariants. 6
Background image of page 6
Exceptions Determining legitimate output for illegitimate input There are three general strategies for determining legitimate output for illegitimate input: 2. “I Give up!” However, it is Not Nice to terminate a program this way.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}