ch8 - Chapter 8 Exceptions Topics Errors and Exceptions...

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

View Full Document Right Arrow Icon
Chapter 8 Exceptions
Background image of page 1

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

View Full DocumentRight Arrow Icon
Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions
Background image of page 2
Exceptions An exception represents an error condition that can occur during the normal course of program execution. see Ch8Sample1 When an exception occurs, or is thrown , the normal sequence of flow is terminated. The exception-handling routine is then executed; we say the thrown exception is caught .
Background image of page 3

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

View Full DocumentRight Arrow Icon
Handling Exceptions We can increase our programs’ reliability and robustness if we catch the exceptions ourselves using error recovery routines we develop. One way to do this is to wrap the statements that may throw an exception with the try- catch control statement.
Background image of page 4
Catching Exceptions inputStr = JOptionPane.showInputDialog(null, prompt); try { age = Integer.parseInt(inputStr); } catch (NumberFormatException e){ JOptionPane.showMessageDialog(null, “’” + inputStr + ‘ is invalid\n” + “Please enter digits only”);
Background image of page 5

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

View Full DocumentRight Arrow Icon
Execution Sequence Statements in the try block are executed in sequence. When one of the statements throws an exception, control is passed to the matching catch block and statements inside the catch block are executed. The execution then continues to the statement following the try -block statement, ignoring any remaining statements in the try block. If no statements in the try block throw an exception, the catch block is ignored. Execution continues with the statement following the try- catch statement.
Background image of page 6
try-catch Control Flow try-catch statement with one catch block.
Background image of page 7

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

View Full DocumentRight Arrow Icon
Throwable Methods There are two methods of the Throwable class we can call to get information about the thrown exception: getMessage printStackTrace
Background image of page 8
Run-Time Stack The run-time stack is what is used to keep track of the methods that are currently being executed. When you call a method, the system puts the parameter data and the location the method was called from on the top of a data structure called a stack. When the method finishes, this data is removed from the stack and the calling method continues at the line the method was called from.
Background image of page 9

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

View Full DocumentRight Arrow Icon
An exception can be thrown in a program using the throw statement. throw <a throwable object>
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/18/2011 for the course COMPUTER S CSC113 taught by Professor Shah during the Spring '09 term at King Saud University.

Page1 / 35

ch8 - Chapter 8 Exceptions Topics Errors and Exceptions...

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

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