Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.

Similar presentations


Presentation on theme: "Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle."— Presentation transcript:

1 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

2 Java’s exception heirarchy Object Throwable Exception

3 Class Throwable public Throwable() public Throwable(String message) public String getMessage() public void printStackTrace() public void printStackTrace(PrintWriter stream) public String toString()

4 Java’s exception classes Exception –Public Exception(String str) ArithmeticException ArrayIndexOutOfBoundsException FileNotFoundException IllegalArgumentException NullPointerException NumberFormatException StringIndexOutOfBoundsException RuntimeException

5 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

6 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 }

7 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)

8 When to omit a throws clause For unchecked exceptions When a checked exception is handled within the method itself, and is not rethrown

9 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

10 Re/Throwing an Exception: Syntax throw new ExceptionClass( msgstring); catch ( ExceptionClass exceptionname) { throw exceptionname; }

11 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

12 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); }


Download ppt "Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle."

Similar presentations


Ads by Google