Week5 - Chapter 8 Exception Handling CS 180 Sunil Prabhakar...

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

View Full Document Right Arrow Icon
Chapter 8 Exception Handling CS 180 Sunil Prabhakar Department of Computer Science Purdue University
Background image of page 1

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

View Full DocumentRight Arrow Icon
Clarifications Auto cast from char to String does not happen. Cast between int and char happens automatically. Class is a data type. It is possible to get the class of any object. Part of Reflection in Java Advanced topic 2
Background image of page 2
3 When things go wrong Good programs should be robust -- I.e., they should be able to handle exceptional situations. What happens if we are trying to input an integer value and the user enters “ten”, or “3.45”? A good program should tell the user to re-enter a valid integer. So far, a situation such as this would result in the termination of our program when we execute Integer.parseInt() on this invalid string. How do we prevent this?
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 Handling errors One idea is to use if -then style tests whenever we expect that an error may arise. This is the style in C -- return values can signal the existence of an error. But this is clumsy, and inelegant. In Java, the exception handling mechanism is used instead. Unexpected (or unusual) cases are handled by a special type of control flow.
Background image of page 4
5 Exceptions An exception is used to indicate that something unusual (that prevents regular processing) has occurred. When an exception occurs, or is thrown , an Exception object is created, and the normal sequence of flow is terminated. An exception handling mechanism is invoked which is responsible for handling or catching the thrown exception.
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Uncaught Exceptions When an exception is thrown, and the program does not specify how to catch it, it causes the program to terminate: import javax.swing.*; public class Test{ public static void main(String[] args){ String inputStr; int i; inputStr = JOptionPane.showInputDialog( null , “Enter Deposit Amount”); i = Integer.parseInt(inputStr); } }
Background image of page 6
Catching an exception 7 String inputStr; int i; inputStr = JOptionPane.showInputDialog( null , Enter an integer ”); try { i = Integer.parseInt(inputStr); } catch (Exception e){ System.out.println(“ Invalid integer ”); }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Exception control-flow 8 . . . try { . . . stmt; . . . } catch (Exception e){ . . . } . . . . . . try { . . . stmt; . . . } catch (Exception e){ . . . } . . . Exception is thrown when executing this statement. No exception Exception thrown
Background image of page 8
9 Exception object An exception is thrown by creating an Exception object. The exception object is passed to the catch block as a parameter. It contains details about the actual exception that was thrown. try { . . . } catch ( Exception e ){ . . . } e is a catch block parameter corresponding to the exception object.
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Exception object The exception object contains details about the exception. The
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 03/26/2012 for the course STAT 350 taught by Professor Staff during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 36

Week5 - Chapter 8 Exception Handling CS 180 Sunil Prabhakar...

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