P. 1
exception in java

exception in java

|Views: 7|Likes:
Published by Vijay Sundar
exception in java
exception in java

More info:

Published by: Vijay Sundar on Feb 05, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

06/15/2015

pdf

text

original

JAVA CORE

EXCEPTION HANDLING

By: Subramanian Date: 15 July 2008

 . including its type and the state of the program when the error occurred.   The object. Creating an exception object and handing it to the runtime system is called throwing an exception. the method creates an object and hands it off to the runtime system. which occurs during the execution of a program. called an exception object. contains information about the error. When an error occurs within a method. that disrupts the normal flow of the program's instructions.EXCEPTION HANDLING  Definition: An exception is an event.

Runtime exceptions are those indicated by RuntimeException and its subclasses. and their subclasses. RuntimeException.    . Errors and runtime exceptions are collectively known as unchecked exceptions. except for those indicated by Error. Errors are not subject to the Catch or Specify Requirement. All exceptions are checked exceptions. Runtime exceptions are not subject to the Catch or Specify Requirement. Errors are those exceptions indicated by Error and its subclasses.The Three Kinds of Exceptions  Checked exceptions are subject to the Catch or Specify Requirement.

4. If no matching catch is found. The nearest enclosing try block is inspected to see if it has a catch statement that matches the type of the exception. 3. control is transferred to that statement. If it does find a match. The general forms of throw is shown here  Throw throwableInstance. However. . using the throw statement. The flow of execution tops immediately after the throw statement. then the default exception handler halts the program and prints the stack trace. and so on. 1.Throw  You have only been catching exception that are thrown by the java run time system. then the next enclosing try statement is inspected. If not. any subsequent statement are not executed. 2. it is possible for your program to throw an exception explicitly.

o. } } p v s m(S args[]){ try{ demoproc(). throw e.Class throwdemo{ static vodi demoproc(){ try{ Example throw new NullPointerException(“Demo”). }catch(NullPointerException e){ S.o.p(”Caught inside demoproc. } } } .p(”Recaught :”+e).”). }catch(NullPointerException e){ S.

} Example: public void writeList() throws IOException. this is necessary for all exception. 2. } .ArrayIndexOutOfBoundsException { //Statement. or any of their sub classes. Type method-name(Parameter-list) throws exception list { //body of the method. it must specify this behavior so that callers of the method can guard themselves against that exception. except those of type Error or Runtime Exception.Throws 1. might throw. You do this by including a throws clause in the method. If a method is capable of causing an exception that is does not handle.

or break. continue. Putting cleanup code in a finally block is always a good practice. This ensures that the finally block is executed even if an unexpected exception occurs. even when no exceptions are anticipated.Finally   The finally block always executes when the try block exits. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return.   .

myexception(int a){ detail=a. you will probably want to create your own exception types to handle situation specific to your applications. }   Class myexception extends Exception { public String toString(){ return “MyException [“ + detail +”]”.Creating Your Own Exception Subclasses  Although java’s built-in exception handle most common errors. This is quite easy to do: just define a subclass of Exception. } } . The following example declares a new subclass of Exception and then uses that subclass to signal an error condition in a method. private int detail.

o.p(“Called Compute(“+a+”)”). }catch (myexception e){ S.o. if (a>10) throw new myexception(a). } p s v m(S args[]) { try{ cmpute(1). } } } .p(“Caught “+e).o. S.p(“Normal Exit”). compute(20).Class exceptiondemo { static void compute(int a) throws myexception { S.

imagine a situation in which a method throws an ArithmeticException because of an attempt to divide by zero. Throwable getCause() 2. This second exception describes the cause of the first exception. which caused the divisor to be set improperly. Chained Exception let you handle this.   Although the method must certainly throw an ArithmeticException. since that is the error that occurred. However. you might also want to let the calling code know that underlying cause was an I/O error. the actual cause of the problem was that an I/O error occurred.   For example. 1. any any other situation in which layers of exception exist. The chained exception methods added to Throwable are getCause() and initCause(). Throwable initCause(Throwable causeExc)   .Chained Exception  The chained exception feature allows you to associate another exception with an exception.

null is returned. The initCause() method associated causeExc with the invoking exception and returns a reference to the exception. If there is no underlying exception .  The getCause() method returns the exception that underlies the current exception. .

getCause()). } } } .p(“Caught:” + e).// Demonstrate exception chaining. Class ChainExcDemo{ static void demoproc(){ //crete an exception NullPointerException e = new NullPointerException(“Top layer”). /*display cause exception*/ S. }catch(NullPointerException e){ /*display top level exception*/ S.o. /*add a cause*/ e.o.initCause(new ArithmeticException(“Cause”).p(“Original Cause: “ + e. trrow e. } p s v m(S args[]){ try{ demoproc().

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->