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 DocumentRight 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 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 3
Background image of page 3

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

View Full DocumentRight 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? 5
Background image of page 5

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

View Full DocumentRight 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. There are some situations where this type of
Background image of page 7

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

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

This note was uploaded on 01/19/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 30

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

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online