lec15 - CS240: Programming in C Lecture 15: Exception...

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

View Full Document Right Arrow Icon
CS240: Programming in C Lecture 15: Exception Handling Monday, April 4, 2011
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 An exception is a control-flow mechanism that allows unexpected (and perhaps erroneous) conditions to be caught and handled Key property: the handler for an exception need not be lexically apparent at the point where the exception is raised Concept: lexical vs. dynamic scope 2 Monday, April 4, 2011
Background image of page 2
The Call Stack 3 Suppose function f calls function g which then calls function h f g h provide a handler for exception E raise exception E: control directly reverts to the handler defined in f f Monday, April 4, 2011
Background image of page 3

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

View Full DocumentRight Arrow Icon
.. 4 public void divide(int numberToDivide, int numberToDivideBy) throws BadNumberException{ if(numberToDivideBy == 0){ throw new BadNumberException("Cannot divide by 0"); } return numberToDivide / numberToDivideBy; } public void f(){ try { int result = g(2,1); System.out.println(result); } catch (BadNumberException e) { //do something clever with the exception System.out.println(e.getMessage()); } System.out.println("Division attempt done"); public void g(int x, y){ .... r = divide(x,y);
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2012 for the course CS 240 taught by Professor Rego during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 13

lec15 - CS240: Programming in C Lecture 15: Exception...

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

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