Title: SE1020
1More Exception Handling
2A few of 60 exception classes
3Catch or let go?
- The Error class represents serious problems that
should not be caught by ordinary applications - (e.g. thread death, out-of-memory).
- The Exception class represents error conditions
that should be caught - (e.g. divide-by-0, null reference, conversion
errors).
4Catching different types of Exceptions
- When there are multiple catch blocks in a
try-catch statement, they are checked in
sequence. - It is important to check more specialized
exception classes before the more general
exception classes. - When an exception is thrown, its matching catch
block is executed and the other catch blocks are
ignored.
5Fumbling to the VM
- If none of the catch blocks matches the thrown
exception, the system will search down the stack
trace for a method with a matching catch block. - If none is found, the system (VM) will handle the
thrown exception - And terminate your program after calling
printStackTrace()
6Getting information from exceptions
- There are two methods of the Throwable class that
provide information about the thrown exception - getMessage a String description
- printStackTrace call stack dump
7Catching Exceptions
- If there is a block of code that must be executed
regardless of whether an exception is thrown, we
use the reserved word finally - inputStr JOptionPane.showInputDialog(null, )
- try
- number Integer.parseInt(inputStr)
- return// will not return until finally is
processed - catch (NumberFormatException e)
- System.out.println(Cannot convert to int)
- return // will not return until finally is
processed - finally
- System.out.println(DONE)
8You can also throw exceptions
- An exception is thrown using the throw statement.
- throw lta throwable objectgt
- where lta throwable objectgt is an instance of the
Throwable class or its subclasses. - Exception is a subclass of Throwable
- By convention, we create Exceptions rather than
Throwable objects
9A few of 60 exception classes
Throwable is the root of all Exceptions and Errors
Your program cannot catch or throw Errors
Your program can catch or throw Exceptions
10Throwing your own Exceptions
- inputStr JOptionPane.showInputDialog(null,
Enter a value less than 100) - number Integer.parseInt(inputStr)
- if (numgt100)
- throw new Exception(Out of bounds)
-
11Passing the buck
- When a method may throw an exception, either
directly or indirectly, the method is called an
exception thrower. - An exception thrower method can be a
- Catcher if it catches it
- Propagator if it lets it go
- Can be both if it catches some types and lets
others go
12Catcher/Propagator Example
- inputStr JOptionPane.showInputDialog(null,
- Enter a value less than 100)
- try
- number Integer.parseInt(inputStr)
- if (numgt100)
- throw new Exception(Out of bounds)
-
- catch (NumberFormatException e)
- System.out.println(Cannot convert to int)
Here, NumberFormatExceptions are caught but
Exceptions are propagated.The Out of bounds
exception will not be caught here!
13RuntimeException a special class of Exception
- Some exceptions have to be caught by your code
and not allowed to propagate to the VM - A checked exception is an exception that is
checked at compile time - RuntimeException and classes that derive from it
are unchecked. - They are detected only at runtime
14Only RuntimeExceptions can be left unchecked
RuntimeExceptions and descendants are unchecked
exceptions
Exception and IOException are checked exceptions
15Propagating Exceptions
- If a method is a checked exception propagator,
its header must be modified to declare the type
of exceptions the method propagates. - Without the required throws clause, the program
will not compile. - The reserved word throws is used for this
declaration. - void SomeMethod( ) throws Exception // some
code that may cause an Exception - // that is not caught here in the method. . .
-
16Callers of a method that can throw a checked
exception must include the try-catch statement in
the method body or the throws clause in the
header.
17Throws is optional for RuntimeExceptions
- For the exceptions of the type (or derived from
the type) called RuntimeException, the throws
clause is optional. - If a method is a propagator of runtime exceptions
or errors, the throws clause is optional.
18- It is optional for callers of a method that can
throw runtime exceptions to include the try-catch
statement in the method body or the throws clause
in the header.