JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Try, Catch and Finally in Exception Handling

January 20, 2014 by Krishna Srinivasan Leave a Comment

The basic procedure to handle the exception is to use the try-catch block. If you want to put any code that would cause a exception, then write that piece of code inside the try-catch block. If you expect multiple type of exceptions in the same block, then you can write more than one catch block for a single try. Not that, it is always only one try which can have multiple catch blocks.

  • At a time only one Exception is occurred and at a time only one catch block is executed.
  • All catch blocks must be ordered from most specific to most general. Otherwise a compiler error will be thrown.
  • If you have a scenario where a piece of code has to be executed even if there is an exception. For example, you have opened file or database resources which has to be cleaned up before shut down the program. Writing that code in the catch block is not good practice since we don’t know what is the exception will be thrown. We have to write a finally block after the catch block which will be executed if any exception thrown, then control goes to the catch block.

Try, Catch and Finally Example

Look at the example program:

[code lang=”java”]
package javabeat.net.core;

public class ExceptionSample {
public void method(){
try{
int value = 10/0;
}catch (ArithmeticException arithmeticException){
arithmeticException.printStackTrace();
}catch (Exception exception){
exception.printStackTrace();
}finally {
System.out.println("Finally Executed");
}
}
public static void main(String args[]){
ExceptionSample exceptionSample = new ExceptionSample();
exceptionSample.method();
}
}
[/code]

Output for the above program will be:

[code]
Finally Executed
java.lang.ArithmeticException: / by zero
at javabeat.net.core.ExceptionSample.method(ExceptionSample.java:6)
at javabeat.net.core.ExceptionSample.main(ExceptionSample.java:17)
[/code]

Filed Under: Java Tagged With: Java Basics

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved