Try/ catch/ finally statement in java

PROBLEM

You wish to use the try/ catch statement in Java. This blog post is showing few simple usages of this powerful mechanism.

solution

Using the try/catch has many use cases as explained in this post. Here we will see the syntax of such a mechanism

try/ catch example
				
					package com.programmerabroad;

public class TryCatchExample {
    public static void main(String[] args) {
        //simple try/catch statement
        try {
            throw new Exception();
        }catch (Exception e) {
            System.err.println("Exception thrown and caught here!");
        }
    }
}

				
			
output
output of exception catch
try/ catch/ finally with exception example

The try/ catch statement can be extended and have a finally block. The finally block is executed despite an Exception thrown or not. Let’s see an example below:

				
					package com.programmerabroad;

public class TryCatchFinallyExample {
    public static void main(String[] args) {
        //try/catch/finally statement
        try {
            System.out.println("Trying to run some sensitive code...");
            throw new Exception();
        }catch (Exception e) {
            System.err.println("Exception thrown and caught here!");
        }finally {
            System.out.println("This is the finally block");
        }
    }
}

				
			
output
output of try/catch/finally statement

The finally block is executed even if the Exception is thrown.

try/ catch/ finally no exception example

Below it’s the same example but no Exception is thrown

				
					package com.programmerabroad;

public class TryCatchFinallyNoExceptionExample {
    public static void main(String[] args) {
        //try/catch/finally statement
        try {
            System.out.println("Trying to run some sensitive code that is fine...");
        }catch (Exception e) {
            System.err.println("Exception thrown and caught here!");
        }finally {
            System.out.println("This is the finally block");
        }
    }
}

				
			
output

As expected, the finally block is still executed.

try/ catch/ finally example continued

Let’s see an example that the finally block will not be executed! Check the code below:

				
					package com.programmerabroad;

public class TryCatchFinallyNoExecutionExample {
    public static void main(String[] args) {
        try {
            System.exit(0);
        }catch (Exception e) {
            System.err.println("Exception thrown and caught here!");
        }finally {
            System.out.println("This is the finally block");
        }
    }
}

				
			
output
output of finally not executed

In this case, the finally statement is not executed because at line 6 the program is terminating. This is a usual interview question.

conclusion

Using this statement we can design more user-friendly programs that guide the user with a nice error rather than crashing the program.  As we can see the finally block is executed despite of an Exception thrown. That is helpful at times, for example, you wish to close a resource like a database connection.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

I eat all cookies

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x