cannot initialise variable in try block?

N

Nandan

The java compiler (1.4.2) won't compile the foll, it says cl may not
have been initialized.

Class cl;
try {
cl = gui.getClass();
Method hidden = cl.getMethod("isVisible", null);
Boolean falsebool = new Boolean(false); //for shame
if((Boolean) hidden.invoke(gui,null)==falsebool) { //lordy!
System.err.println("Obj is hidden " + key);
continue;
}
Object o = valuemethodmap.get(cl);
if (o == null)
continue;
String mtname = o.toString();
/*
* System.err.println("ID " + key + " isa " + gui.getClass() + "
* and methodmap gives " + mtname);
*/
Method mt = cl.getMethod(mtname, null);
Object retval = mt.invoke(gui, null);
Element guielem = new Element("control").setAttribute("name",
key.toString());
Element tentsel = new Element("contents").setAttribute(
"method", mtname);
tentsel.setText(retval.toString());
guielem.addContent(tentsel);
problem.addContent(guielem);
} catch (NoSuchMethodException nsme) {
System.err.println("Method " + nsme + "not found for" + cl);
}


How can a catch statement be reached without at least one statement being
(willan on-being) executed? Is this in the JVM spec or something, that
exceptions may (say) be raised in another thread or something?

Or is this another discipline issue, like not compiling on unreachable code?

--
Nandan Bagchee

So I think people who are mature and experienced, with a business background,
may be overrated. We used to call these guys 'newscasters', because they had
neat hair and spoke in deep, confident voices, and generally didn't know much
more than they read on the teleprompter.

-- Paul Graham
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Nandan said:
The java compiler (1.4.2) won't compile the foll, it says cl may not
have been initialized.

Class cl;
try {
cl = gui.getClass();
} catch (NoSuchMethodException nsme) {
System.err.println("Method " + nsme + "not found for" + cl);
}

How can a catch statement be reached without at least one statement being
(willan on-being) executed?

If getClass() in the code above would throw NoSuchMethodException, no
value would be assigned to cl, since the assignment would never take
place. That is the reason you get the error.

To fix it, move the "cl = gui.getClass();" line outside the try block.
In general, don't put non-throwing code in try blocks.
 
T

Tom McGlynn

Daniel said:
If getClass() in the code above would throw NoSuchMethodException, no
value would be assigned to cl, since the assignment would never take
place. That is the reason you get the error.

To fix it, move the "cl = gui.getClass();" line outside the try block.
In general, don't put non-throwing code in try blocks.

Just to expand on this point...

You're the one who said that the compiler shouldn't rely on the following
code executing. You put it in the try block. So the compiler is just
doing what you told it. That's what it means to put it in the try block.
"Try this code but carefully and be ready to handle an exception at any point."

Note that if gui==null then you will indeed manage to fail before cl gets initialized.

One quick way to get the code to work without moving things around, is to
simply set cl to null when you define it. Being equal to null is not the
same as being unitialized.

By the by, the error message probably needs to be something like

"Method "+mename+" not found for "+cl+". Exception: "+nsme

since nsme is not the name of the method, but the exception you
got when you looked for it. But you'll need to define mtname
before the try block then too.

Regards,
Tom McGlynn
 
T

Tor Iver Wilhelmsen

Nandan said:
Class cl;
try {
cl = gui.getClass();

Just use "Class cl = null;" for that first line - method-local
variables do not have the same default initialization as fields have.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top