Objectives Understanding what an exception is Understanding the

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 get. Message() public void print. Stack. Trace(Print. Writer stream) • public String to. String()

Java’s exception classes • Exception – Public Exception(String str) • • Arithmetic. Exception Array. Index. Out. Of. Bounds. Exception File. Not. Found. Exception Illegal. Argument. Exception Null. Pointer. Exception Number. Format. Exception String. Index. Out. Of. Bounds. Exception Runtime. Exception

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( Exception. Class. Name 1 obj. Ref 1) { // exception handler 1 } finally { // other statements executed regardless of // whether the exception is thrown or not // normally used for cleanup }

When to use 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 Exception. Class( msgstring); • catch ( Exception. Class 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 My. Div. By. Zero. Exception extends Exception { public my. Div. By. Zero. Exception() { super(“Cannot divide by zero”); } public my. Div. By. Zero. Exception(String msg) { super(msg); } }
- Slides: 12