Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 18 May 2013

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,
try{
// java code lines
}
finally{
// 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.
try{
// java code lines
}
catch(Exception e){

}
finally{
// 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) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public void calledMethod() throws IOException{
  BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
  bfr.readLine(); // 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.








Thanks for reading !
Being Java Guys Team


18 comments:

  1. nice explanation!!!!!!

    ReplyDelete
  2. clear explanation... Too simple and easy to understand...

    ReplyDelete
  3. Thanks !!!! found clear explanation

    ReplyDelete
  4. nice. for more java examples, visit http://java2novice.com site

    ReplyDelete
  5. Thanks U very much...!!!

    ReplyDelete
  6. Very good!
    Thanks!

    ReplyDelete
  7. Good explanation.

    ReplyDelete
  8. DeenDayal Yadav13 June 2015 at 02:21

    Excellent.......Expanation

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft