chapter14 - Chapter14ExceptionHandling 1 1 Usingtry and

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

View Full Document Right Arrow Icon
1 Chapter 14 - Exception Handling Using  try and catch  Blocks to Handle "Dangerous" Method  Calls NumberFormatException Line Plot Example try  and  catch  Blocks - More Details Two Types of Exceptions - Checked and Unchecked Unchecked Exceptions Checked Exceptions Using API Documentation when Writing Exception-Handling  Code When a  try  Block Throws Different Types of Exceptions The  Exception  Class and its  getMessage  Method Multiple  catch  blocks Understanding Exception Messages 1
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 Using  try  and  catch  Blocks to Handle "Dangerous" Method Calls Some API method calls are "dangerous" in that they  might possibly lead to a runtime error. Example of a "safe" API method call (no runtime error  possible): System.out.println( <expression> ) Example of an API method call that might lead to a  runtime error: Integer.parseInt( <string> ) Technique for handling such runtime errors: Use  exception handling . More specifically, surround the  method call with a  try  block and insert a  catch  block  immediately after the  try  block. 1 2 3
Background image of page 2
3 Using  try  and  catch  Blocks to Handle "Dangerous" Method Calls Syntax for  try and catch  blocks: try { <statement(s)> } catch ( <exception-class> <parameter> ) { <error-handling-code> } Example  try and catch  code fragment: try { quantity = Integer.parseInt(userEntry); } catch (NumberFormatException nfe) { System.out.println("Invalid quantity entered." + " Must be a whole number."); } Normally, one or more of these statements will be a  "dangerous" API method call or constructor call. The exception class should match the type of  exception that the  try  block might throw. 1 2 3
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 Using  try  and  catch  Blocks to Handle "Dangerous" Method Calls Semantics for previous slide's  try and catch  code  fragment: If the  userEntry  string contains all digits, then: quantity  gets the  int  version of  userEntry . The JVM skips the  catch  block and continues below it. If the  userEntry  string does not contain all digits, then: The  parseInt  method  throws  a  NumberFormatException   object. The JVM looks for a  catch  block that will catch the thrown  exception object; i.e., it looks for a matching  catch  block. If it  finds one, it executes it and continues below the  catch  block. If  there's no matching  catch  block, the program crashes. 1 2
Background image of page 4
5 NumberFormatException The  NumberFormatException  is well named  because it's thrown when a number's format is  inappropriate. More specifically, it's thrown by one of the parse 
Background image of page 5

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

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

This note was uploaded on 10/22/2010 for the course COMPSCI CS201 taught by Professor Sorenson during the Spring '10 term at Wisconsin Milwaukee.

Page1 / 40

chapter14 - Chapter14ExceptionHandling 1 1 Usingtry and

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

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