10+-+Exceptions - 5/17/2009 Exceptions Motivation Ve 280...

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

View Full Document Right Arrow Icon
5/17/2009 1 Exception Ve 280 Programming and Introductory Data Structures Exceptions Exceptions Motivation y We want a means of recognizing and handling unusual conditions in your program at runtime, not just at compile time. y If you have a function that does not produce meaningful results for all possible values of its inputs, then it is a partial functions . y We have seen one particular way of preventing a partial function from receiving inputs for which it cannot deliver meaningful results: the REQUIRES specification. y However, a REQUIRES clause is just comments and cannot enforce the specification. Therefore, it is easy to pass parameters that violate the specification. This isn’t desirable… Exceptions Motivation y Instead of the REQUIRES clause, let’s look at another way of ensuring correct inputs: runtime checking . y 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. y One nice things about REQUIRES, is that we don’t have to figure out what constitutes “bad” input. With runtime checking, we do… / Exceptions Determining legitimate output for illegitimate input y There are three general strategies for determining legitimate output for illegitimate input: 1. “It’s my problem!” y 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. y For example, list_rest() could return an empty list if presented with an empty list. y Such behavior must be explained in the specification! Exceptions Determining legitimate output for illegitimate input y There are three general strategies for determining legitimate output for illegitimate input: 1. “It’s my problem!” y However, this strategy fails whenever the function cannot implement its specification with the given inputs. y For example, what is factorial to do if it is passed a negative integer? y Factorial is simply undefined for negative numbers, and trying to define it changes the rules of math. Exceptions Determining legitimate output for illegitimate input y There are three general strategies for determining legitimate output for illegitimate input: 2. “I Give up!” y Use something like assert() or exit() . y assert(condition) is a c-library function that says: “terminate the program if condition is not true.” It is useful for checking invariants. list_rest (list_t l) // REQUIRES: list is not empty { assert(!list_isEmpty(l)); }
Background image of page 1

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

View Full DocumentRight Arrow Icon
5/17/2009 2 Exceptions Determining legitimate output for illegitimate input y There are three general strategies for determining legitimate output for illegitimate input: 2. “I Give up!” y However, it is Not Nice to terminate a program this way.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/26/2009 for the course EECS 280 taught by Professor Jeff during the Summer '09 term at University of Michigan-Dearborn.

Page1 / 5

10+-+Exceptions - 5/17/2009 Exceptions Motivation Ve 280...

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

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