ExceptionsNew

ExceptionsNew - Exception Handling CS 180 Sunil Prabhakar...

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

View Full Document Right Arrow Icon
Exception Handling CS 180 Sunil Prabhakar Department of Computer Science Purdue University Monday, March 19, 2012
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 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, this situation would result in the termination of our program when we execute Integer.parseInt() on this invalid string. How do we prevent this? Monday, March 19, 2012
Background image of page 2
3 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. Monday, March 19, 2012
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 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. Monday, March 19, 2012
Background image of page 4
5 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 ReadInt{ public static void main(String[] args){ String inputStr; int i; inputStr = JOptionPane.showInputDialog( null , "Enter Deposit Amount"); i = Integer.parseInt(inputStr); } } Monday, March 19, 2012
Background image of page 5

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

View Full DocumentRight Arrow Icon
Catching an exception 6 String inputStr; int i; inputStr = JOptionPane.showInputDialog( null , "Enter an integer" ); try { i = Integer.parseInt(inputStr); } catch (Exception e){ System.out.println( "Invalid integer" ); } Monday, March 19, 2012
Background image of page 6
Exception control-flow 7 . . . try { . . . stmt; . . . } catch (Exception e){ . . . } . . . . . . try { . . . stmt; . . . } catch (Exception e){ . . . } . . . Exception is thrown when executing this statement. No exception Exception thrown Monday, March 19, 2012
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 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. Monday, March 19, 2012
Background image of page 8
9 Exception object The exception object contains details about the exception. The getMessage() method simply returns a string of text that describes the exception. The
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 41

ExceptionsNew - Exception Handling CS 180 Sunil Prabhakar...

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

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