throw, throws and finally keyword in Java Examples - Exception Handling in Java - Part 3/3

Welcome to final part of Exception handling in java tutorial series. In previous discussions we came across fundamentals of exception handling in java and other useful information about exception handling. Then we came across how to use try-catch in java and their examples. In this particular blog we will discuss throws, throw and finally keywords and their examples in Java.

3) 'finally' keyword in java

Sometimes we have situations when a certain piece of code must be executed, no matters if try block is executed successfully or not, this is what a ?finally? block does for us.

A ?finally? block is written followed by a try block or a catch block, code written inside a finally block is always executed. See the example below:

3.1) Using ?finally? with a try only

As we have discussed earlier, a try catch mush have atleast one catch or finally block with it. Here is the syntax of using a try followed by finally block,
   // java code lines
   // java code lines that would always be executed, no matters what happened inside try block

3.2) Using ?finally? with try-catch combination

A finally block can be used with a combination of try-catch blocks, see the example below.
   // java code lines
}catch(Exception e){
   // java code lines that would always be executed, no matters what happened inside try block

4 ) 'throws' keyword in Java

In java if a code written within a method throws an exception, there can be two cases. Either the code is being enclosed by try block and exception handling is implemented in the same method, or the method can throws the exception to the calling method simply.

This is what ?throws? does in exception handling, it throws the exception to immediate calling method in the hierarchy. The throws keyword appears at the end of a method's signature. See the example below:
public void callingMethod() {
 try {
  calledMethod(); // here the exception is handled   
 } catch (IOException e) {
  k e.printStackTrace();
public void calledMethod() throws IOException {
 BufferedReader bfr = new BufferedReader(new InputStreamReader(;
 // because of throws keyword the exception occured here will be handled by calling method 
The calling method itself can use a throws keyword and forward the exception to handle by its own calling method.. so on . This can be forwarder up in the hierarchy, if all calling methods are using throws and no more calling method is available to handle the exception that the exceptions are ultimately be handled by main method. And if the main method is also using an throws itself the exception will be handled by JVM itself.

5) 'throw' keyword in Java

In java it?s possible to throw an exception programmatically , throws keyword is used to throw an exception explicitly. Using throws we handle certain situations in our program. Here is an example that will validate if the age enter by the user is more than 18 or not, see the example below:
public void saveUserAge(int age) {
 if (age < 18) {
  throw new ArithmeticException();

 } else {
  System.out.println("Correct are is entered");


Important notes about Exception Handling in Java

1) A ?try? block must be followed by a catch or a finally block.
2) A ?try? block can contain any number of catch block, but these catch blocks must represent distinct Exception Classe that too in narrow to broad order.
3) A ?catch? or ?finally? block can not be used without a ?try? block.
4) There can not be any type of code in between a try, catch and finally blocks

Here we are done with our Exception Handling in Java tutorial series, in upcoming blogs we will cover more about java and other open source technologies.