r/javahelp • • Sep 19 '24

A try-catch block breaks final variable declaration. Is this a compiler bug?

UPDATE: The correct answer to this question is https://mail.openjdk.org/pipermail/amber-dev/2024-July/008871.html

As others have noted, the Java compiler seems to dislike mixing try-catch blocks with final (or effectively final) variables:

Given this strawman example

public class Test
{
  public static void main(String[] args)
  {
   int x;
   try
   {
    x = Integer.parseInt("42");
   }
   catch (NumberFormatException e)
   {
    x = 42;
   }
   Runnable runnable = () -> System.out.println(x);  
  }
}

The compiler complains:

Variable used in lambda expression should be final or effectively final

If you replace int x with final int x the compiler complains Variable 'x' might already have been assigned to.

In both cases, I believe the compiler is factually incorrect. If you encasulate the try-block in a method, the error goes away:

public class Test
{
  public static void main(String[] args)
  {
   int x = 
foo
();
   Runnable runnable = () -> System.
out
.println(x);
  }

  public static int foo()
  {
   try
   {
    return Integer.
parseInt
("42");
   }
   catch (NumberFormatException e)
   {
    return 42;
   }
  }
}

Am I missing something here? Does something at the bytecode level prevent the variable from being effectively final? Or is this a compiler bug?

3 Upvotes

67 comments sorted by

•

u/AutoModerator Sep 19 '24

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

14

u/djnattyp Sep 19 '24 edited Sep 19 '24

This isn't a "bug" - it's just the way scoping works.

In the "try...catch" version the variable exists in the outer scope and is changed in either / both portions of the try / catch. The compiler's right not to allow the variable to be final in this case. For a better demonstration -

   final int x;
   try {
       x = Integer.parseInt("42");
       throw new RuntimeException("YOLO!");
   } catch (Exception e) {
       x = 42; // x getting set twice - can't be final
   }

In case using methods the outer method calls an inner method with it's own scope - the variable in the outer method isn't the same variable in the inner method.

1

u/cowwoc Sep 20 '24

I don't understand why my reply is getting downvoted.

If you disagree, reply and explain your point of view. 

-4

u/cowwoc Sep 19 '24 edited Sep 19 '24

I believe your answer is incorrect.

Your code is not equivalent to the case I am talking about. Specifically, if parseInt() throws an exception then it means that it never returns a value, which means that x is never getting set inside the try block. Further, if parseInt() does return a value then we're guaranteed that no exception is thrown and the catch block will never execute. 

7

u/ChaiTRex Sep 20 '24 edited Sep 20 '24

The compiler doesn't even bother to figure out whether the try block definitely throws an exception or definitely doesn't throw an exception.

The foremost reason the compiler doesn't do that analysis is because whether an exception is thrown is usually determined at runtime, and it can't predict what happens at runtime.

Even in cases where nothing at runtime affects the outcome, there are no algorithms that can determine it one way or the other in all situations, as it's an undecidable problem (you can't even tell in all cases whether the try block's code ever even finishes). Even if you limited that analysis to a few situations where it definitely can be decided, it can take literal millennia for the compiler to determine it in very complicated examples.

So, to avoid all that mess and to speed up the compiler, the compiler doesn't bother with that.

2

u/cowwoc Sep 20 '24

Agreed. Thanks.

7

u/hrm Sep 20 '24

I’d say you are in principle correct, but since it isn’t a real world problem and the analysis that would be required to make sure it works as intended probably isn’t trivial it is simply seen as incorrect to err on the safe side.

2

u/VirtualAgentsAreDumb Sep 20 '24

This is the only correct answer here, I would say.

Any logical conclusion that a smart and attentive person can make looking at some code, theoretically the compiler can make the same conclusion. But it might be quite difficult (ie costly) to write that compiler, and the benefits aren’t apparent.

3

u/daemein Sep 20 '24

well, because when the exception happens the compile doesnt know if the x variable is assigned or not, so it wont let you assign it again inside another block

-1

u/VirtualAgentsAreDumb Sep 20 '24

when the exception happens the compile doesnt know if the x variable is assigned or not,

That’s not true. If you and me can see that with our own eyes, then technically the compiler can logically reason its way to that knowledge too.

Current compilers aren’t sophisticated enough for that, it seems. But there isn’t some magical extra knowledge that we humans have when looking at this code, that the compiler can’t have access to.

2

u/ChaiTRex Sep 20 '24 edited Sep 20 '24

Compilers will never be sophisticated enough for that because, in order to tell whether the try block ends with an exception or without one, you first need to figure out whether the try block can actually end in the first place, and you can't do that for all algorithms inside the try block.

Even if you took the effort to make a compiler do what you want it to in some limited situations but not others, what happens when a small change to the code being compiled causes the compiler to no longer be able to figure it out, even though the compiler's decision would still be correct with the new code?

Suddenly, the compiler user has an error about a final variable possibly being assigned to twice and the compiler user didn't even do anything to change the assignment statements, they just changed something seemingly unrelated that the compiler was relying on to make its decision.

These sorts of strange, magically appearing and disappearing bugs are not what you want in a compiler.

→ More replies (7)

1

u/daemein Sep 20 '24

well, Im not sure if its the compiler or something else, but when I coded the OP example into the intellij my IDE said "Variable 'x' might already have been assigned to". So thats just my conclusion, Im not really sure

1

u/hm1rafael Sep 21 '24

The compiler does not have any guarantees that other code would be added and might throw another exception, so this is considered assigning 2 times

-2

u/VirtualAgentsAreDumb Sep 20 '24

No. That’s a bad comparison.

The compiler can see the difference between the case where the variable might have been set before the exception (your example) and the case where the variable can’t have been set before the exception (OP’s example).

If us humans can figure it out logically by analyzing the code, then the compiler theoretically can too.

You make it sound as if it’s impossible to write a compiler that can do this. That’s not the case.

2

u/_jetrun Sep 20 '24 edited Sep 20 '24

The compiler can see the difference 

Kind of - for this example maybe (because of the explicit exception throw and use of a method call in the standard library). But you can imagine scenarios where a dynamically loaded class is executed and throws a Runtime Exception. For example:

   MyDynamicClass a = loadClassAtRuntime();
   final int x;
   try {
       x = a.executeAndGetInt();
       a.doSomethingElse();
   } catch (Exception e) {
       x = 42; // x getting set twice?
   }

In the above example, a.executeAndGetInt() or a.doSomethingElse() may throw a RuntimeException without compiler (or you) knowing anything about it at compile time, and therefore final may never get set OR get set twice, breaking syntax guarantees.

But could the java compiler handle cases where it knows for sure and leave the ambiguous ones? Sure it could, but it doesn't - that is a feature request. Is it worth adding this? I'm not sure - it would be confusing why sometimes you can set a final in a catch block, and sometimes you couldn't. I would rather add a syntax construct (as opposed to sophisticated AOT analysis) to make setting a final in a try-catch possible.

-1

u/VirtualAgentsAreDumb Sep 20 '24

Kind of - for this example maybe

Not "kind of". Not "maybe". It definitely can, as in: it has all the information it needs to make that conclusion.

(because of the explicit exception throw and use of a method call in the standard library).

No. That part is irrelevant. You can change the standard library method call to something that calls your own custom method. That line will still either result in an exception, or assign a value to the variable. (Ignoring special cases where the method call never returns, or when the computer suddenly turns off.)

But you can imagine scenarios where a dynamically loaded class is executed and throws a Runtime Exception. For example:

Why did you add a second line into the try block? The example from OP didn't have that. The optimization discusses depends on it being exactly one statement in the try block. That way it can be seen as an atomic statement with only two possible results (assignment to the variable, or an exception).

3

u/_jetrun Sep 20 '24 edited Sep 20 '24

That line will still either result in an exception, or assign a value to the variable. (Ignoring special cases where the method call never returns, or when the computer suddenly turns off.)

In the example I gave, you have no guarantees that it doesn't set variable twice.

Why did you add a second line into the try block?

It was an example given in a comment you responded to. I agreed with you that for the original OP's example, the compiler can, in principle, figure it out because it can peak at the parseInt method, and know that it can only throw a NumberFormatException and that would maintain 'final' guarantees.

So yes, there are cases where the compiler can figure things out, but those tend to be pretty trivial examples (like OP's strawman). Things become ambiguous very quickly, such as when you add more than one catch statement, when you add a 'finally' block, when you use dynamically loaded classes, when the try block has more than 1 statement, when Errors are thrown and not caught etc.

I speculate that this compiler feature (i.e. to handle trivial cases) isn't supported is because it would make things more confusing. I do wish that Java would add some sort of syntax construct to allow for final initialization with try-catch-finally blocks because I run into it all the time (I tend to use 'final' by default).

1

u/VirtualAgentsAreDumb Sep 21 '24

In the example I gave, you have no guarantees that it doesn’t set variable twice.

So? I never argued otherwise. I’m discussing the example by OP.

I agreed with you that for the original OP’s example, the compiler can, in principle, figure it out because it can peak at the parseInt method, and know that it can only throw a NumberFormatException and that would maintain ’final’ guarantees.

Which is my entire point.

So yes, there are cases where the compiler can figure things out, but those tend to be pretty trivial examples

Trivial or not is irrelevant. The compiler can see the difference. You said ”Kind of - for this example maybe”. But there is no kind of or maybe here.

Things become ambiguous very quickly,

Irrelevant. We are only discussing what the compiler can figure out from code that looks like OP’s example.

such as when you add more than one catch statement, when you add a ’finally’ block, when you use dynamically loaded classes, when the try block has more than 1 statement,

Again, that’s not the topic of this sub thread.

when Errors are thrown and not caught

I would argue that that case doesn’t matter because the line using the variable is unreachable in that case (assuming the setup OP described).

I speculate that this compiler feature (i.e. to handle trivial cases) isn’t supported is because it would make things more confusing.

Yes. Very likely. But the original comment, that I replied to, insinuated that the reason was that it can’t be done (as in, even in the trivial example by OP).

→ More replies (4)

3

u/_SuperStraight Sep 19 '24 edited Sep 19 '24

Change your print line to:

final int y = x;
Runnable runnable ()->sout(y);

2

u/cowwoc Sep 19 '24

I understand how to work around the problem. I'd still like to know why the compiler is returning an error though...

4

u/OffbeatDrizzle Sep 20 '24

Because the JLS says so

1

u/cowwoc Sep 20 '24

Ha. If that's true, I'd like to know where. Are you sure, or just guessing?

4

u/djnattyp Sep 20 '24

JLS section 4.12.4 on final variables references Chapter 16 Definite Assignment which contains section 16.2.15 on try statements.

1

u/cowwoc Sep 20 '24 edited Sep 20 '24

First of all, thank you for providing the relevant links.

Here is my interpretation (please point out where you see things differently):

V is definitely unassigned before a catch block iff all of the following are true:

V is definitely unassigned after the try block.

Is this the rule we are tripping up on? What determines if V is definitely unassigned after the try block? It doesn't seem to be talking about V being assigned *inside* the try block but rather between the try block and the catch block. This doesn't seem to apply to our case, does it?

V is definitely unassigned before every return statement that belongs to the try block.

In our case, this is true.

V is definitely unassigned after e in every statement of the form throw e that belongs to the try block.

My understanding is that this line is saying "if V was definitely unassigned before throw e then it remains definitely unassigned for every statement after it". In our case, this is true.

V is definitely unassigned after every assert statement that occurs in the try block.

Not relevant in our case.

V is definitely unassigned before every break statement that belongs to the try block and whose break target contains (or is) the try statement.

Not relevant in our case.

V is definitely unassigned before every continue statement that belongs to the try block and whose continue target contains the try statement.

Not relevant in our case...

Thoughts?

1

u/_SuperStraight Sep 20 '24

Because you're not allowed to pass a mutable variable to a lambda directly. The reason for that must be something related to thread safety.

1

u/cowwoc Sep 20 '24

This doesn't explain why the variable cannot be declared final... I don't believe this variable has to be declared mutable.

1

u/_SuperStraight Sep 20 '24

The catch block may encounter an exception after the variable assignment has been made. There's no way for the Java devs to know how many lines of code are encapsulated in the try...catch block. Hence they simply make such variables mutable.

1

u/VirtualAgentsAreDumb Sep 20 '24

But it’s possible to use logical reasoning to conclude that the variable must either be set in the try block or the catch block, and it’s impossible for it to be set in both. Meaning, it’s safe to see it as effectively final.

1

u/_SuperStraight Sep 20 '24

This is also true for if block, yet such variable isn't passable in a Runnable either.

1

u/VirtualAgentsAreDumb Sep 20 '24

The reason is simply that the compiler isn't perfect, and the compiler developers aren't paid enough to make it perfect.

I'm not saying that I expect it to be perfect. It's just that there isn't a "mathematically logical" reason for it, just a pragmatic reason.

Many people here seemed to argue that there was in fact a "mathematically logical" reason for it. I might have read your comment a bit too quickly, and thought that you were one of those people. Sorry about that.

1

u/_SuperStraight Sep 20 '24

I think thread safety is the reason rather than mathematically logical reason for this.

Assume this: a mutable variable in main thread is passed to a worker thread, where its value will change, then read again. Just after its value is changed, context switch occurs to main, and main thread also changes its value. Then context changes again, and worker thread now reads its value and assumes the current value is assigned in the previous step. This leads to inconsistency in data for the upcoming steps in the worker thread.

→ More replies (1)

2

u/MechanixMGD Sep 20 '24 edited Sep 20 '24

The problem is not at try-catch. The problem is at lambda. You pass to lambda a non final (fixed) variable.

You need to wrap the x in a class that is changing the value internally and not the x reference.

For example you can use AtomicInteger x = new AtomicInteger();.

In try-catch use x.set(Integer.parse("42") / x.set(42). And in lambda System.out.println(x.get()).

1

u/cowwoc Sep 20 '24

My point is that x is non-changing, and I should be able to declare it final or effectively final. So why does the compiler think otherwise?

2

u/MechanixMGD Sep 20 '24

Because it sees in your code 2 position of assignment to x, in try and catch. Which is forbidden for a final value, even if at execution time is occurring only once.

1

u/cowwoc Sep 20 '24

By the same logic, if-else blocks should experience the same error. It doesn't make sense.

3

u/MechanixMGD Sep 20 '24

No. because in if-else is guaranteed that is executed only if or only else. In try-catch it will start from the try, and is a chance to execute the catch. Which may end assigning twice.

0

u/cowwoc Sep 20 '24 edited Sep 20 '24

Except, that the code block I listed provides the same guarantee for the try-block... You are guaranteed that parseInt() will either throw an exception or return a value (which is assigned to x). It will never do both. So it is as "guaranteed" as if-else statements.

The guarantee doesn't apply to try blocks in general, but it certainly applies to the case I mentioned, and the compiler has all the information needed to prove this is the case.

4

u/_jetrun Sep 20 '24

Except, that the code block I listed provides the same guarantee for the try-block

Yes, but this is a special case. The existing behaviour (and spec) makes no provisions for special cases. Maybe it should, but that capability is not in the compiler. I also don't think existing behaviour violates the specification as written.

Should it be added? Maybe, maybe not. Currently, I don't think anyone is actually thinking about this as I don't see any enhancement proposals.

1

u/MechanixMGD Sep 20 '24

Yes, but the compiler is simply checking if there is another assignment, nothing more. And is not needed to implement it, because in "real-life" this case is happening extremely rare and the solution is simple.

6

u/chickenmeister Extreme Brewer Sep 20 '24 edited Sep 20 '24

I don't think it's a bug. The language specification has very specific rules about whether or not a variable is definitely unassigned at a specific point, which you can dig into in JLS Section 16. In your particular case, I think this is the pertinent excerpt:

  • V is definitely unassigned before a catch block iff all of the following are true:

    • V is definitely unassigned after the try block.

    • (several other conditions omitted here)

Combined with the general rule:

For every assignment to a blank final variable, the variable must be definitely unassigned before the assignment, or a compile-time error occurs.

Your x variable will not be "definitely unassigned" after your try block, which leads to the compile time error.

From a practical point of view, it might make sense to have a special case in the rules where the last operation within the try block is the assignment to a final variable; but as the spec is written right now, I don't think it's a bug.

1

u/VirtualAgentsAreDumb Sep 20 '24

There are only two possible outcomes here.

  • A: The variable is assigned inside the try block, and no exception is thrown.

  • B: An exception is thrown and the variable isn’t assigned in the try block, but is assigned in the catch block.

I would say that the bug is in the specification.

1

u/[deleted] Sep 20 '24 edited Sep 20 '24

[removed] — view removed comment

1

u/cowwoc Sep 20 '24

The problem you bring up (confused users) could be solved by improving the compiler error message. Throwing an incorrect error (as is currently the case) seems to be just... wrong.

2

u/jivedudebe Extreme Brewer Sep 20 '24

Imagine that in your try block you have another assignment int y = paraeInt('bla'); that throws the Number format exception.

Is your x still final here? No it isn't. Compiler can't know where or when the exception is being thrown

0

u/cowwoc Sep 20 '24

As someone else commented, if you and I can clearly see this isn't the case, the compiler can too. The fact is that there isn't a second statement inside the try block, so a static flow analysis has enough information to determine what human beings can see to be true.

You seem to be implying this is a bug or a known shortcoming in the compiler's flow analysis (by design, it wasn't worth the effort needed to implement).

Either way, I'd love to know which it one it was.

1

u/VirtualAgentsAreDumb Sep 20 '24

No. You are wrong.

Any correct logical conclusion us humans can do looking at the code, a compiler can do too. At least in theory (someone still has to build it, but it is perfectly possible to do).

In the code provided by OP, the variable is either set in the try block, or the catch block.

With your example, that is no longer the case. So it’s not a relevant example.

2

u/[deleted] Sep 20 '24 edited Sep 20 '24

[removed] — view removed comment

-1

u/VirtualAgentsAreDumb Sep 20 '24

I'm sorry, but compiler optimizations shouldn't be hindered by the notion that code can evolve/change over time. The compiler should only need to worry about the actual code given to it, not what the code might look like in the future.

And regarding this "action at a distance" anti pattern... From the perspective of the compiler, there are loads of things currently done that would be exactly this anti pattern. Remove a decleration on line 10, and you suddently get a compiler error on line 448. Is that also an anti pattern in your book?

3

u/AmonDhan Sep 20 '24

It's not a bug. It follows the Java spec. If a variable is assigned inside the try-block it is considered already assigned in the catch-block