Exception Handling (Throws and Finally Keyword)
Exception Handling (Throws and Finally Keyword)
finally keyword)
Throws
• If a method is capable of causing an exception
that it does not handle, it must specify this
behavior so that callers of the method can
guard themselves against that exception.
• A throws clause lists the types of exceptions
that a method might throw.
• This is necessary for all exceptions, except
those of type Error or RuntimeException, or
any of their subclasses.
• All other exceptions that a method can throw
must be declared in the throws clause. If they
are not, a compile-time error will result.
This is the general form of a method declaration
that includes a throws clause:
type method-name(parameter-list) throws
exception-list
{
// body of method
}
Program
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);
}
}
}
Here is the output generated by running this example program:
inside throwOne
caught java.lang.IllegalAccessException: demo
Finally keyword
• finally creates a block of code that will be executed after a
try/catch block has
• completed and before the code following the try/catch
block.
• The finally block will execute whether or not an exception
is thrown. If an exception is thrown, the finally block will
execute even if no catch statement matches the
exception.
• Any time a method is about to return to the caller from
inside a try/catch block, via an uncaught exception or an
explicit return statement, the finally clause is also
executed just before the method returns.
• This can be useful for closing file handles and
freeing up any other resources that might
have been allocated at the beginning of a
method with the intent of disposing of them
before returning.
• The finally clause is optional. However, each
try statement requires at least one catch or a
finally clause.
Program
// Demonstrate finally.
class FinallyDemo {
// Through an exception out of the method.
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
} finally {
System.out.println("procA's finally");
}
}
// Return from within a try block.
static void procB() {
try {
System.out.println("inside procB");
return;
} finally {
System.out.println("procB's finally");
}
}
// Execute a try block normally.
static void procC() {
try {
System.out.println("inside procC");
} finally {
}
}
public static void main(String args[]) {
try {
procA();
} catch (Exception e) {
System.out.println("Exception caught");
}
procB();
procC();
}
}
• In this example, procA( ) prematurely breaks
out of the try by throwing an exception.
• The finally clause is executed on the way out.
procB( )’s try statement is exited via a return
• statement. The finally clause is executed
before procB( ) returns. In procC( ), the try
statement
• executes normally, without error. However,
the finally block is still executed.
If a finally block is associated with a try, the finally block
will be executed upon
conclusion of the try.
Here is the output generated by the preceding program:
inside procA
procA’s finally
Exception caught
inside procB
procB’s finally
inside procC
procC’s finally