Try with finally without catch in java

The finally Block

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return , continue , or break . Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Note: The finally block may not execute if the JVM exits while the try or catch code is being executed.

The try block of the writeList method that you’ve been working with here opens a PrintWriter . The program should close that stream before exiting the writeList method. This poses a somewhat complicated problem because writeList ‘s try block can exit in one of three ways.

  1. The new FileWriter statement fails and throws an IOException .
  2. The list.get(i) statement fails and throws an IndexOutOfBoundsException .
  3. Everything succeeds and the try block exits normally.
Читайте также:  Coding java in cmd

The runtime system always executes the statements within the finally block regardless of what happens within the try block. So it’s the perfect place to perform cleanup.

The following finally block for the writeList method cleans up and then closes the PrintWriter and FileWriter .

Important: Use a try- with-resources statement instead of a finally block when closing a file or otherwise recovering resources. The following example uses a try -with-resources statement to clean up and close the PrintWriter and FileWriter for the writeList method:

Источник

Guide to the Java finally Keyword

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We’re looking for a new Java technical editor to help review new articles for the site.

1. Overview

In this tutorial, we’ll explore the finally keyword in Java. We’ll see how to use it alongside try/catch blocks in error handling. Though finally is intended to guarantee the execution of code, we’ll discuss exceptional situations in which the JVM does not execute it.

We’ll also discuss some common pitfalls where a finally block can have an unexpected outcome.

2. What Is finally?

finally defines a block of code we use along with the try keyword. It defines code that’s always run after the try and any catch block, before the method is completed.

The finally block executes regardless of whether an exception is thrown or caught.

2.1. A Quick Example

Let’s look at finally in a try-catch-finally block:

try < System.out.println("The count is " + Integer.parseInt(count)); >catch (NumberFormatException e) < System.out.println("No count"); >finally

In this example, regardless of the value of the parameter count, the JVM executes the finally block and prints “In finally”.

2.2. Using finally Without a catch Block

Also, we can use a finally block with a try block regardless of whether a catch block is present:

Inside try Inside finally

2.3. Why finally Is Useful

We generally use the finally block to execute clean up code like closing connections, closing files, or freeing up threads, as it executes regardless of an exception.

Note: try-with-resources can also be used to close resources instead of a finally block.

3. When finally Is Executed

Let’s have a look at all the permutations of when the JVM executes finally blocks, so we can understand it better.

3.1. No Exception Is Thrown

When the try block completes, the finally block is executed, even if there was no exception:

In this example, we aren’t throwing an exception from the try block. Thus, the JVM executes all code in both the try and finally blocks.

Inside try Inside finally

3.2. Exception Is Thrown and Not Handled

If there’s an exception and it is not caught, the finally block is still executed:

The JVM executes the finally block even in the case of an unhandled exception.

Inside try Inside finally Exception in thread "main" java.lang.Exception

3.3. Exception Is Thrown and Handled

If there’s an exception and it is caught by the catch block, the finally block is still executed:

try < System.out.println("Inside try"); throw new Exception(); >catch (Exception e) < System.out.println("Inside catch"); >finally

In this case, the catch block handles the thrown exception, and then the JVM executes the finally block and produces the output:

Inside try Inside catch Inside finally

3.4. Method Returns from try Block

Even returning from the method will not prevent finally blocks from running:

Here, even though the method has a return statement, the JVM executes the finally block before handing the control over to the calling method.

Inside try Inside finally

3.5. Method Returns from catch Block

When the catch block contains a return statement, the finally block is still called:

try < System.out.println("Inside try"); throw new Exception(); >catch (Exception e) < System.out.println("Inside catch"); return "from catch"; >finally

When we throw an exception from the try block, the catch block handles the exception. Though there is a return statement in the catch block, the JVM executes the finally block before handing control over to the calling method, and it outputs:

Inside try Inside catch Inside finally

4. When finally Isn’t Executed

Although we always expect the JVM to execute the statements inside a finally block, there are some situations where the JVM will not execute a finally block.

We might already expect that if the operating system stops our program, then the program would not get the chance to execute all of its code. There are also some actions we can take that will similarly prevent the execution of a pending finally block.

4.1. Invoking System.exit

In this case, we are terminating the JVM by calling System.exit and hence the JVM will not execute our finally block:

4.2. Invoking halt

Similar to System.exit, a call to Runtime.halt also halts the execution and the JVM does not execute any finally blocks:

4.3. Daemon Thread

If a Daemon thread enters the execution of a try/finally block and all other non-daemon threads exit before the daemon thread executes the finally block, the JVM doesn’t wait for the daemon thread to finish the execution of finally block:

Runnable runnable = () -> < try < System.out.println("Inside try"); >finally < try < Thread.sleep(1000); System.out.println("Inside finally"); >catch (InterruptedException e) < e.printStackTrace(); >> >; Thread regular = new Thread(runnable); Thread daemon = new Thread(runnable); daemon.setDaemon(true); regular.start(); Thread.sleep(300); daemon.start();

In this example, the runnable prints “Inside try” as soon as it enters the method and waits for 1 second before printing “Inside finally”.

Here, we start the regular thread and the daemon thread with a small delay. When the regular thread executes the finally block, the daemon thread is still waiting within the try block. As the regular thread completes execution and exits, the JVM also exits and does not wait for the daemon thread to complete the finally block.

Inside try Inside try Inside finally

4.4. JVM Reaches an Infinite Loop

Here’s a try block which contains an infinite while loop:

Though it’s not specific to finally, it’s worth mentioning that if the try or catch block contains an infinite loop, the JVM will never reach any block beyond that loop.

5. Common Pitfalls

There are some common pitfalls that we must avoid when we use the finally block.

Although it’s perfectly legal, it’s considered bad practice to have a return statement or throw an exception from a finally block, and we should avoid it at all costs.

5.1. Disregards Exception

A return statement in the finally block ignores an uncaught exception:

In this case, the method ignores the RuntimeException thrown and returns the value “from finally”.

5.2. Ignores Other return Statements

A return statement in the finally block ignores any other return statement in the try or catch block. Only the return statement in the finally block executes:

In this example, the method always returns “from finally” and completely ignores the return statement in the try block. This could be a very difficult bug to spot, which is why we should avoid using return in finally blocks.

5.3. Changes What’s Thrown or Returned

Also, in the case of throwing an exception from a finally block, the method disregards the exception thrown or return statements in the try and catch blocks:

This method never returns a value and always throws a RuntimeException.

While we may not intentionally throw an exception from the finally block as in this example, we may still encounter this issue. It can occur when cleanup methods we use in a finally block throw an exception.

6. Conclusion

In this article, we discussed what finally blocks do in Java and how to use them. Then, we looked at different cases where the JVM executes them, and a few when it might not.

Lastly, we looked at some common pitfalls associated with using finally blocks.

As always, the source code used in this tutorial is available over on GitHub.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Оцените статью