Thursday, November 4, 2010

Exception Hnadling in Java

Flow Control and Exception Handling - try-catch-finally
try {
} catch (exceptionType1 identifier1) {      // one or multiple
} catch (exceptionType2 identifier2) {
} finally {                                 // one or none

must include either one catch clause or a finally clause can be multiple catch clauses but only one finally clause
the try statements are executed until an exception is thrown or it completes successfully a compile-error occurs if the code included in the try statement will never throw one of the caught checked exceptions (runtime exceptions never need to be caught)if an exception is thrown, each catch clause is inspected in turn for a type to which the exception can be assigned; be sure to order them from most specific to least specific when a match is found, the exception object is assigned to the identifier and the catch statements are executed if no matching catch clause is found, the exception percolates up to any outer try block that may handle it a catch clause may throw another exception if a finally clause is included, it's statements are executed after all other try-catch processing is complete the finally clause executes whether or not an exception
is thrown or a break or continue are encountered .

Flow Control and Exception Handling - Exceptions


An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

  • exceptions provide a clean way to check for errors
  • they are an explicit part of a methods contract

exceptions are thrown at runtime if errors occur when a class is loaded or during method execution
runtime exceptions are objects of the classes java.lang.RuntimeException, java.lang.Error or their subclasses runtime exceptions are also called unchecked exceptions

code may also throw an exception using the throw statement
these are non-runtime or checked exceptions
any exceptions you create in your code should extend java.lang.Exception which implements the interface java.lang.Throwable
you create your own exceptions to add useful data to an error message or, if you are interested in a particular error

both forms of exceptions (checked and unchecked) may be caught and handled in exception-handling code
an uncaught exception is caught by a default handler which halts execution and displays an error message
exception handling is done using the try-catch-finally statment

Flow Control and Exception Handling - Exception Handling

  • methods must declare which checked exceptions they may throw in their throws clause
  • public void methodName throws Exception1, Exception2,()

you do not have to include any checked exception which will be caught and handled within the method
a method can throw multiple exceptions
a method can only throw exceptions that have been declared in the throws clause
an overriding method cannot throw any checked exceptions which are not part of the original methods throws clause
the throws clause must also include any possible exceptions that can be thrown by the method
if you invoke a method that has a checked exception in its throws clause you can
catch and handle the exception
catch it and throw one of the exceptions listed in the method throws clause
declare the exception in your throws clause
a method which does not have a throws clause may still throw unchecked exceptions or errors
these exceptions and errors can occur at any time, in any code

Standard Unchecked Exceptions:

ArithmeticException                 IllegalTrheadStateException
ArrayStoreException                 IndexOutOfBoundsException 
ClassCastException                  MissingResourceException  
EmptyStackException                 NegativeArraySizeException
IllegalArgumentException            NoSuchElementException
IllegalMonitorStateException        NullPointerException 
IllegalStateException               NumberFormatException
Standard Unchecked Errors:

AbstractMethodError                 NoSuchFieldError
ClassFormatError                    NoSuchMethodError
ExceptionInInitializerError         OutOfMemoryError
IllegalAccessError                  StackOverflowError
IncompatibleClassChangeError        ThreadDeath
InstantiationError                  UnknownError
InternalError                       UnsatisfiedLinkError
LinkageError                        VerifyError
NoClassDefFoundError                VirtualMachineError

Static initializers, instance initializers, and class or variable initializers must not produce any checked exceptions
exceptions are thrown using the throw statement

throw Expression;   
throw new ExampleException();

or by invoking a method that throws an exception
the expression must be an instance of a Throwable object ie the exception class must implement Throwable

No comments:

Post a Comment