Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

Does `finally` Run Before `return` in Java? What `javac` Does

Java executes an applicable finally block before a pending return reaches the caller. A normal completion preserves the return; a return or exception from finally can replace it.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Yes. When a return leaves a try or catch, Java runs any applicable finally block before the method returns to its caller. If the block finishes normally, the pending return proceeds. If it returns or throws instead, that abrupt completion can replace the pending return.

What “before return” means

The return statement starts the method’s return, but control does not transfer to the caller until the applicable finally clauses have run. With nested try statements, those clauses run from the innermost outward.

As an Amazon Associate I earn from qualifying purchases.

The Java Language Specification defines this behavior; it is not an optional feature of javac. The relevant rules are in the Java SE 26 Language Specification sections on return statements and execution of try–finally statements.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What happens to the pending return

If finally completes normally

The original return continues after the finally block finishes. For example, this method returns 7:

static int answer() {
    try {
        return 7;
    } finally {
        System.out.println("cleanup");
    }
}

The message is printed before the caller receives the returned value.

If finally returns

A return inside finally replaces the pending return. In this example, the method returns 0, not 1:

static int answer() {
    try {
        return 1;
    } finally {
        return 0;
    }
}

That behavior is legal, but it can hide the original return value or suppress an exception. Avoid returning from finally in ordinary application code.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If finally throws

An exception thrown by finally likewise interrupts the pending return. The caller observes the exception rather than the value that the try or catch was preparing to return, unless the exception is handled before it escapes.

When is the return value determined?

The return expression is evaluated as part of executing the return statement, before control enters the finally block. A normally completing finally does not retroactively recalculate that expression just because it changes a local variable afterward.

static int answer() {
    int value = 7;
    try {
        return value;
    } finally {
        value = 9;
    }
}

This method returns 7. The expression value has already supplied the value for the pending return when finally assigns 9. This is distinct from returning a mutable object reference: changing the referenced object in finally can affect the object the caller later observes, even though the reference expression was already evaluated.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What javac does

The Java SE 21 javac manual documents -Xlint:finally, which warns about finally clauses that cannot complete normally. Its example has a return 1 in a catch and a return 0 in finally; the method returns 0.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This is a warning, not a compile error, and it does not change Java’s runtime rules. The language specification determines the observable behavior; the compiler option helps identify a risky pattern.

Why JVM implementation details are a separate issue

The Java Virtual Machine Specification, Java SE 26, discusses how a compiler can arrange finally code along paths that leave a try statement, including normal and abrupt transfers. That is an implementation explanation, not a guarantee that every compiler or class-file version uses one particular sequence of instructions. For Java source, the dependable rule is the language-level one: applicable finally code runs before control reaches the invoker, and its completion determines whether the pending return proceeds.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.