Throwable instance in JAVA

 
Creating Exceptions







Two ways to obtain a Throwable instance:

1) creating one with the new operator All Java built-in exceptions have at least two Constructors:

One without parameters and another with one String
parameter:

 throw new NullPointerException("demo");


2) using a parameter of the catch clause


try { ... } catch(Throwable e) { ... e ... }


Example: throw 1


class ThrowDemo {
//The method demoproc throws a NullPointerException
exception which is immediately caught in the try block and
re-thrown:
static void demoproc() {
try {
throw new NullPointerException("demo");
} catch(NullPointerException e) {
System.out.println("Caught inside demoproc.");
throw e;
}
}


Example: throw 2


The main method calls demoproc within the try block which catches and handles the NullPointerException exception:


public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}


throws Declaration


• If a method is capable of causing an exception that it does not handle, it must specify this behavior by the throws clause in its declaration:


type name(parameter-list) throws exception-list {
...
}


• where exception-list is a comma-separated list of all types of
exceptions that a method might throw.


• All exceptions must be listed except Error and RuntimeException or any of their subclasses, otherwise a compile-time error occurs.


Example: throws 1


• The throwOne method throws an exception that it does not
catch, nor declares it within the throws clause.


class ThrowsDemo {
static void throwOne() {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
throwOne();
}
}


• Therefore this program does not compile.


Example: throws 2


• Corrected program: throwOne lists exception, main catches it:


class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
} } }


finally

• When an exception is thrown:

1) the execution of a method is changed

2) the method may even return prematurely.

• This may be a problem is many situations.

• For instance, if a method opens a file on entry and closes on
exit; exception handling should not bypass the proper closure
of the file.

• The finally block is used to address this problem.


 Any time a method is to return to a caller from inside the

try/catch block via:

1) uncaught exception or

2) explicit return

the finally clause is executed just before the method returns.


Example: finally 1


• Three methods to exit in various ways.

class FinallyDemo {

//procA prematurely breaks out of the try by throwing an

exception, the finally clause is executed on the way out:

static void procA() {

try {

System.out.println("inside procA");

throw new RuntimeException("demo");

} finally {

System.out.println("procA's finally");

} }


Example: finally 2


// procB’s try statement is exited via a return statement, the

finally clause is executed before procB returns:


static void procB() {

try {

System.out.println("inside procB");

return;

} finally {

System.out.println("procB's finally");

}

}


Example: finally 3


• In procC, the try statement executes normally without error,

however the finally clause is still executed:


static void procC() {

try {

System.out.println("inside procC");

} finally {

System.out.println("procC's finally");

}

}


Example: finally 4


• Demonstration of the three methods:


public static void main(String args[]) {

try {

procA();

} catch (Exception e) {

System.out.println("Exception caught");

}

procB();

procC();

}

}

Post a Comment (0)
Previous Post Next Post