Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » The final local variable output may already have been assigned in a try catch block(In a try / catch on a specific exception type (that can be raised only by a given method), the compiler thinks a variable can be assigned twice - I fail to see how that would be possible.)
icon5.gif  The final local variable output may already have been assigned in a try catch block [message #634644] Fri, 22 October 2010 14:54 Go to next message
Olivier Cailloux is currently offline Olivier CaillouxFriend
Messages: 43
Registered: July 2009
Member
Hello,

How is it possible that the variable in this example be assigned twice?
    public void testExc() {
	final int c;
	try {
	    c = doTheMaths();
	} catch (MyCheckedException exc) {
	    c = 0;
	}
    }
    private int doTheMaths() throws MyCheckedException {
	return -1;
    }
    public class MyCheckedException extends Exception {
    }


The compiler says "The final local variable c may already have been assigned".

A similar question has been asked (here), but in my case this is a checked exception. I don't see how it could be raised after the assignment, hence, after the return from the method. Does the Java spec really mandate that behavior? Does it make sense?

Thanks.
Olivier
Re: The final local variable output may already have been assigned in a try catch block [message #634675 is a reply to message #634644] Fri, 22 October 2010 17:18 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
This has nothing to do with the eclipse compiler.
This is simply the JLS that requires this code to be flagged as an error.
You can check that javac behaves the same as the Eclipse compiler.
--
Olivier
Re: The final local variable output may already have been assigned in a try catch block [message #634678 is a reply to message #634675] Fri, 22 October 2010 17:26 Go to previous message
Olivier Cailloux is currently offline Olivier CaillouxFriend
Messages: 43
Registered: July 2009
Member
But my question was about the why of this behavior...

(I admit, considering your answer, that the question does maybe not belong to the eclipse forum specifically, but I thought I would find here some java experts that would be able to provide an explanation...)
Previous Topic:Using ASTParser outside Eclipse
Next Topic:Eclipse installation AFTER plugins installation still moveable to another dir?
Goto Forum:
  


Current Time: Fri Apr 26 11:21:43 GMT 2024

Powered by FUDForum. Page generated in 0.03112 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top