Download checked exceptions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Handling Exceptions
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
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.
3
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.");
}
4
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.");
}
5
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
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
@exception Tag in
Documentation Comments
• General format
@exception ExceptionName Description
• Remember:
@param
@result
@author
@version/date
9