Exceptions
Download
1 / 13

Exceptions - PowerPoint PPT Presentation


  • 71 Views
  • Uploaded on

Exceptions. CIS 304 Intermediate Java Programming for Business. 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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Exceptions' - zarola


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


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

Exceptions

CIS 304 Intermediate Java Programming for Business


Guarding your statements
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


Basic approach
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


Example
Example

try

{price = Double.parseDouble(priceStr);}

catch (NumberFormatException nfe){

JOptionPane.showMessageDialog(

null, "price must be numeric – " +

"reenter and hit 'Submit'");

}


Multiple errors
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


The finally block
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…}


Popular exceptions
"Popular" Exceptions

  • NumberFormatException

  • NullPointerException

  • ArithmeticException

  • ClassCastException

  • ArrayIndexOutOfBoundsException

  • RuntimeException


Two methods to use with exceptions
Two Methods to use with Exceptions

  • Example:

    catch (IOException ioe)

    {

    system.out.println(ioe.printStackTrace());

    system.out.println(ioe.getMessage());

    }


Other possibilities
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…)}


Throwing your own exceptions
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


Checked exceptions
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


In class exercise
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


In class exercise continued
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)


ad