CH08 - Chapter 8 Exception Handling Basic Exception...

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

View Full Document Right Arrow Icon
Chapter 8 Chapter 8 Basic Exception Handling » the mechanics of exceptions Defining and Using Exceptions » some "simple" cases Reality Check » guidelines for more realistic situations Exception Handling
Background image of page 1

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

View Full DocumentRight Arrow Icon
Chapter 8 Exception Handling Overview A way of organizing a program into sections for the normal case and the exceptional case » exception examples: division by zero incorrect type of input A way of implementing programs incrementally » » add code for the exceptional case later Simplifies development, testing, debugging and maintenance » errors are easier to isolate
Background image of page 2
Chapter 8 Warning : The example programs in this chapter are simplified for instructional purposes. Real programs are more complicated and usually have a somewhat different organization. More about this later, after the mechanics of exception handling, their definition and use have been explained
Background image of page 3

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

View Full DocumentRight Arrow Icon
Chapter 8 Some Terminology Throwing an exception : either Java itself or your code signals when something unusual happens Handling an exception : responding to an exception by executing a part of the program specifically written for the exception » also called catching an exception The normal case is handled in a try block The exceptional case is handled in a catch block The catch block takes a parameter of type Exception » it is called the catch -block parameter » e is a commonly used name for it If an exception is thrown, execution in the try block ends and control passes to the catch block(s) after the try block
Background image of page 4
Chapter 8 try-throw-catch Threesome Basic code organization: try { <code to try> if(test condition) throw new Exception("Message to display"); <more code> } catch(Exception e) { <exception handling code> } <possibly more code>
Background image of page 5

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

View Full DocumentRight Arrow Icon
Chapter 8 try-throw-catch Program Flow Try block Statements execute up to the conditional throw statement If the condition is true the exception is thrown » control passes to the catch block(s) after the try block Else the condition is false » the exception is not thrown » the remaining statements in the try block (those following the conditional throw) are executed Catch block Executes if an exception is thrown » may terminate execution with exit statement » if it does not exit, execution resumes after the catch block Statements after the Catch block Executes if either the exception is not thrown or if it is thrown but the catch block does not exit
Background image of page 6
An Example  of Exception  Handling try { System.out.println("Enter number of donuts:");
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.

Page1 / 34

CH08 - Chapter 8 Exception Handling Basic Exception...

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