Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

January 12, 2016 Categories: Exception Handling. No Comments on throw throws and finally

In this article we will look at the use of throw throws and finally keywords of exception handling in Java programs.

 

throw Keyword

The throw keyword can be used in Java programs to throw exception objects explicitly. The syntax of using throw is as follows:

throw ThrowableInstance;

The ThrowableInstance can be object of Throwable class or any of its sub classes. A reference to the Throwable instance can be obtained using the parameter in catch block or by using the new operator.

Let’s see a sample program that demonstrates the use of throw:

At line 7 throw keyword is used to throw a n object of ArithmeticException with the message Testing throw. At line 12 we are re-throwing the exception to Java run-time system. Output of the above program is:

java.lang.ArithmeticException: Testing throw
Exception in thread “main” java.lang.ArithmeticException: Testing throw
at ArrayException.main(ArrayException.java:8)

 

throws Keyword

The throws keyword can be used in method definition to let the caller of a method know about the exceptions that the method might raise and which are not handled by that method. The general form of throws is as follows:

type method-name(parameters-list) throws exception-list

{

//body of method

}

The exception-list is a comma separated list of exceptions that the method might throw. Except the sub classes of Error and RuntimeException classes all other exceptions (checked exceptions) must be mentioned explicitly with the throws keyword. Otherwise, it will lead to compile-time errors.

Let’s see a sample program which demonstrates the use of throws keyword:

Observe that the method myMethod() does not handle ArithmeticException. It throws the exception to its caller (main method) which in turn handles the exception. We can throw multiple exceptions using the throws keyword.

 

finally Keyword

The statements in finally  block are guaranteed to be executed irrespective of whether an exception raises or not. House keeping code like closing the connection to a file or database can be written in a finally block as it always executes.

Let’s see an example program which demonstrates the use of finally keyword:

Output of the above program is:

Value is: 5
Guaranteed to execute

Let’s modify the above program so that an exception is raised:

Output of the above program is:

java.lang.ArrayIndexOutOfBoundsException: 8
Guaranteed to execute

Note that in both cases the statement inside finally block got executed.

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me