Can we have try without catch block in java

In this post, we will see about can we have try without catch block in java.

Yes,  we can have try without catch block by using finally block.
You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.exit().

Lets understand with the help of example.

When you execute above program, you will get following output:

What happens when you have return statement in try block:

If you have return statement in try block, still finally block executes.

When you execute above program, you will get following output:

What happens if you have return statement in finally block too

It overrides whatever is returned by try block. Lets understand with the help of example:

When you execute above program, you will get following output:

What if exception is thrown in try block

If exception is thrown in try block, still finally block executes.

When you execute above program, you will get following output:

As you can see that even if code threw NullPointerException, still finally block got executed.
You can go through top 50 core java interview questions for more such questions.

Related Posts

  • 19 September

    Java try with resources

    Table of ContentsOlder approach to close the resourcesJava 7 try with resourcesSyntaxExampleJava 9 Try with resources ImprovementsFinally block with try with resourcesCreate Custom AutoCloseable Code In this post, we will see about Java try with resources Statement. Java try with resources is a feature of Java which was added into Java 7. It helps to […]

  • Exception Handling Interview Questions
    22 February

    Java Exception Handling Interview Questions And Answers

    Exceptional handling is one of the most important topics in core java. Here is list of questions that may be asked on Exceptional handling. Question 1: What is Exception ? Answer: Java doc says “ An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s […]

  • 15 February

    Difference between throw and throws in java

    Table of Contentsthrow:throws: In this tutorial, we are going to see difference between throw and throws in java. throw: throw keyword is used to throw any custom exception or predefine exception. For example: Let’s say you want to throw invalidAgeException when employee age is less than 18. Create a Employee class as below. [crayon-613b1bcc56fbf368288361/] Create […]

  • 19 October

    Exception handling in java

    Table of ContentsExceptionsWhat is Exception ?Exceptions hierarchyUnchecked ExceptionsErrorsDifference between checked exception, unchecked exception and errorsConclusionReferences  Exceptions I have started writing about the and how to prepare for the various topics related to OCAJP exams in my blog. In my previous post, I have published few sample mock questions for StringBuilder class. In this post I […]

  • 17 July

    How to create custom exception in java

    In this post, we will see how to create custom exception in java. It is very simple to create custom exception in java. You just need to extends Exception class to create custom exception. Lets understand this with example. You have list of counties and if You have “USA” in list of country, then you […]

  • 14 July

    Difference between checked and unchecked exception in java

    In this post, we will see difference between checked and unchecked exception in java. It is important question regarding exceptional handling. What is Exception? Exception is unwanted situation or condition while execution of the program. If you do not handle exception correctly, it may cause program to terminate abnormally. What is checked exception? Checked exceptions […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.