Transcript Mar. 31

Objectives
• Understanding what an exception is
• Understanding the heirarchy of exception
classes
• Learn the types of exception and how to
catch and handle them
• Learn how to throw or rethrow and
exception
Java’s exception heirarchy
Object
Throwable
Exception
Class Throwable
•
•
•
•
•
public Throwable()
public Throwable(String message)
public String getMessage()
public void printStackTrace()
public void printStackTrace(PrintWriter
stream)
• public String toString()
Java’s exception classes
• Exception
– Public Exception(String str)
•
•
•
•
•
•
•
•
ArithmeticException
ArrayIndexOutOfBoundsException
FileNotFoundException
IllegalArgumentException
NullPointerException
NumberFormatException
StringIndexOutOfBoundsException
RuntimeException
Checked and Unchecked Exceptions
• Checked Exceptions
– Any exception that can be checked by the
compiler.
– Require the use of a throws clause
• Unchecked Exceptions
– Not checked by the compiler, may occur at run
time
– To insure correctness of the program these
exceptions should be handled
Try/catch/finally
try
{
//statements that may generate exceptions
}
catch( ExceptionClassName1 objRef1)
{
// exception handler 1
}
finally
{
// other statements executed regardless of
// whether the exception is thrown or not
// normally used for cleanup
}
When to use the throws clause
• We have used the throws clause with our methods,
when those methods could throw a checked
exception. Checked exceptions must be handled
• This allows the method to pass any checked
exception within itself on to the level calling the
method or to Java default exception handlers (for
the main method)
• Because the exceptions were not handled within
the method they needed to be handled outside the
method ( in the calling level or the Java system)
When to omit a throws clause
• For unchecked exceptions
• When a checked exception is handled
within the method itself, and is not rethrown
Throwing or Rethrowing an Exception
• An exception is rethrown by a process when the
catch block in that process, associated with that
exception only partially processes that exception
• Usually occurs when an exception requires local
processing in the present method and further
processing in the calling methods
• You can throw (rethrow) any type of exception
• You can define and throw your own classes of
exceptions
Re/Throwing an Exception: Syntax
• throw new ExceptionClass( msgstring);
• catch ( ExceptionClass exceptionname)
{
throw exceptionname;
}
Exception Handling Techniques
• Terminate the program
– Close/cleanup in the finally block if necessary
– No point in continuing if vital component is
missing
• Fix the error and continue
– Put the try/catch/finally in a while loop which is
exited when the try block executes without
throwing an exception
• Log the error and continue
Creating new exception Classes
• Your new exception class should be inherited from
class Exception
public class MyDivByZeroException extends Exception
{
public myDivByZeroException()
{
super(“Cannot divide by zero”);
}
public myDivByZeroException(String msg)
{
super(msg);
}
}