aspose file tools*
The moose likes Beginning Java and the fly likes Try Blocks and Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try Blocks and "cannot resolve symbol"" Watch "Try Blocks and "cannot resolve symbol"" New topic
Author

Try Blocks and "cannot resolve symbol"

Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
I am currently taking an input using the tokenizer to break it into array elements and then parse into the appropriate int variables.

Of course trying to parse a string of "B" into an Int isn't going to be fun.. In fact it throws "NumberFormatException" so I figured I would put it in a try block and recall my input method in this case.

However I get the Cannot Resolve Symbol Error. This is a quick snip

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

A set of curly braces defines a "scope". A variable name defined inside a scope can't be used outside that scope. So the variable nMsgType, defined inside the "try" block, can only be used inside the "try" block.

You can fix this in two ways. One would be to break the declaration into two lines:



Another way -- and in fact, the way I generally prefer to do this -- is to make your "try" block bigger, so that the "catch" is pushed to the end of the routine, and all the code that used nMsgType moves inside the try block.


[Jess in Action][AskingGoodQuestions]
Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
Another way -- and in fact, the way I generally prefer to do this -- is to make your "try" block bigger, so that the "catch" is pushed to the end of the routine, and all the code that used nMsgType moves inside the try block.


Is this the way it should be done? Do you loose any control in the end having the try block covering a larger area? I do see the advantages though.
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
Having the catch clause pushed down the code allows you to reduce variable scope, but sometimes you have to deal differently with exceptions, so it's all about what goes in the try clause. Speaking with code, it would be a choice between this:


and this:



When you nest a lot of try/catch blocks it may get somewhat confusing (been there, done that), so you ought to do whatever fits you best.


Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try Blocks and "cannot resolve symbol"
 
Similar Threads
parseInt Question.......
keep getting NullPointerException error
How do I compile mt Tag Handler...?
HiLo game and NumberFormatException
parameter passing (very beginner)