Download
exceptions n.
Skip this Video
Loading SlideShow in 5 Seconds..
Exceptions PowerPoint Presentation

Exceptions

76 Vues Download Presentation
Télécharger la présentation

Exceptions

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Exceptions CIS 304 Intermediate Java Programming for Business

  2. Guarding Your Statements • When you anticipate a problem you can write code that will keep the problem from happening • But, can't always do that, because • Don't know what the problem will be • Don't know how to handle it • Will discover the problem too late

  3. Basic Approach • Use two blocks of code • try block • This block contains the code that may cause the exception • catch block • This block contains the code that deals with the exception • May "fix" the error • May write a message to the user • May terminate the program, with or without a message

  4. Example try {price = Double.parseDouble(priceStr);} catch (NumberFormatException nfe){ JOptionPane.showMessageDialog( null, "price must be numeric – " + "reenter and hit 'Submit'"); }

  5. Multiple Errors • Sometimes more than one type of error may be anticipated. If so, • Have several "catch" blocks • Include these in decreasing order of specificity because only the first applicable catch block executes • You want to know as exactly as possible what error occurred, so you can deal with it precisely • If you do the more general first, then you can't get to the specific • The compiler may give you an error saying you can't get to the code in the more specific catch statement

  6. The finally block • You can include code that will be executed regardless of what happens in the try or the catch blocks try {…code that may throw an exception…} catch (…) {…code to deal with exception…} catch (…) {…code to deal with exception…} finally {…code that executes after above…}

  7. "Popular" Exceptions • NumberFormatException • NullPointerException • ArithmeticException • ClassCastException • ArrayIndexOutOfBoundsException • RuntimeException

  8. Two Methods to use with Exceptions • Example: catch (IOException ioe) { system.out.println(ioe.printStackTrace()); system.out.println(ioe.getMessage()); }

  9. Other Possibilities • Instead of catch have method "throw an exception" public double myMethod(int myInt) throws NumberFormatException {…code of method that may generate NumberFormatException…no catch here} ********************************* And then in the code where myMethod is called try {myObject.myMethod(5)} catch (NumberFormatException nfe) {… deal with exception here, such as with error message…)}

  10. Throwing Your Own Exceptions • Your program logic may be able to detect errors that would cause exceptions • Rather than letting the JVM throw the error, your program can do it itself • To do this your code would include, for example throw new NumberFormatException("Value entered was not numeric"); • Advantages • You control the timing • You get to write your own error message, which is the String argument in the constructor above

  11. Checked Exceptions • "Checked Exceptions" are exceptions that Java knows may occur • Java insists that the program be prepared for Checked Exceptions and that these be caught • If the compiler knows that an error may result from an operation, it will insist that the exception be caught, or be declared to be thrown • Failure to do this will cause a compiler error • IOException and its subclasses are checked exceptions

  12. In-Class Exercise • Write a simple application that will ask the user to enter a location and name of a simple .txt file that contains just one record, and open that file. If the file does not exist, then prompt the user to reenter the name and location again. • Write a simple GUI application that will ask the user to enter a number of employees, and the incentive pay pool. The pool is shared evenly. Calculate and display the input values and the per employee payout from the pool. • Make sure that both entries are numeric • Make sure that you handle division by zero

  13. In-Class Exercise (continued) 3. Instead of using try / catch in 2 above, catch the exception in the calling method (and not in the method itself where the exception is detected)