Presentation is loading. Please wait.

Presentation is loading. Please wait.

Handling Exceptions.

Similar presentations


Presentation on theme: "Handling Exceptions."— Presentation transcript:

1 Handling Exceptions

2 Handling Exceptions The parameter must be of a type that is compatible with the thrown exception’s type this means don’t have a catch (ArithmeticException ae) if the exception you expect to occur is a (FileNotFoundException fnfe) because it won’t catch it. After an exception is handled, the program will leave the catch block and continue execution at the point following it. 2

3 Polymorphic References To Exceptions
When handling exceptions, you can use a polymorphic reference (see page 730) as a parameter in the catch clause but do not do so in this course. This means that although you can catch any exception that is derived from the Exception class with catch(Exception e) you should not. The exceptions that you must handle are the checked exceptions . They are derived from the Exception class. There are unchecked exceptions derived from RuntimeException which can be ignored but you should not ignore them in this course. Show it by altering BadArray.java Can use Exception e only to determine what the actual exception was. 3 3

4 Exception Handlers A try statement may have only one catch clause for each specific type of exception. try { number = Integer.parseInt(str); } catch (NumberFormatException nfe) System.out.println("Bad number format."); catch (NumberFormatException nfe) // is an ERROR because NumberFormatException has already been caught System.out.println(str + " is not a number."); There can be many polymorphic catch clauses. 4 4

5 Exception Handlers The NumberFormatException class is derived from the IllegalArgumentException class. this means that the IllegalArgumentException class is more general than the NumberFormatException try { number = Integer.parseInt(str); } catch (IllegalArgumentException e) System.out.println("Bad number format."); catch (NumberFormatException e) // this is an ERROR because the more specific exception follows the more general exception. System.out.println(str + " is not a number."); This will be caught by the compiler because NumberFormatException is derived from IllegalArgumentException (see page 765) Re-ordering the catch blocks will make the code compile 5 5

6 Creating Exception Classes
You can create your own exception classes by deriving them from the Exception class or one of its derived classes. Text example: BankAccount.java NegativeStartingBalance.java AccountTest.java 7

7 Creating Exception Classes
Some examples of exceptions that can affect a bank account: These should be handled by program not as exceptions. A negative starting balance is passed to the constructor. A negative interest rate is passed to the constructor. A negative number is passed to the deposit method. A negative number is passed to the withdraw method. The amount passed to the withdraw method exceeds the account’s balance. We can create exceptions that represent each of these error conditions but that would be stupid. 8

8 @exception Tag in Documentation Comments
General format @exception ExceptionName Description Remember: @param @result @author @version/date The following rules apply tag in a method’s documentation comment must appear after the general description of the method. The description can span several lines. It ends at the end of the documentation comment (the */ symbol) or at the beginning of another tag. 9 9


Download ppt "Handling Exceptions."

Similar presentations


Ads by Google