Can't Access Variables that are inside a try block

I can't access any variables that are inside my try block. Is that normal? Is there anyway to get around that? Thanks for your help.
[140 byte] By [louya] at [2007-10-3 7:44:08]
# 1
A varable's scope is the nearest enclosing block to where it's declared.If you want a variable to be visible both inside the try block and outside of it, you must declare it outside the try block.
jverda at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks for your help.

Can I declare the variable outside the try block, manipulate it inside the try block, and use it outside the try block? (Say it goes in equal to 1, inside the try block it becomes equal to 2, when I use it again outside the try block will it be equal to 2?)

Message was edited by:

louy

louya at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 3

> Thanks for your help.

>

> Can I declare the variable outside the try block,

> manipulate it inside the try block, and use it

> outside the try block? (Say it goes in equal to 1,

> inside the try block it becomes equal to 2, when I

> use it again outside the try block will it be equal

> to 2?)

Yes.

jverda at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 4

I tried that and it is still not working. Maybe my problem is different from what I thought it was originally. This is what is going on: I am parsing a string to an integer. The string is defined inside the try block (it has to be because it is coming from an external file), then I parse the string outside the try block, but it doesn't recognize the original string variable i am trying to parse.

variable strheightRows might not have been initialized

int heightRows = Integer.parseInt(strheightRows);

If I try to get around that and parse the string inside the try block, the program doesn't recognize the newly created integer variable, so I am stuck.

Message was edited by:

louy

louya at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 5
It will be easier if you post more code.
jverda at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 6

Here are my variable declarations & then my try block:

String inputLine, line1, line2, line3;

String strwidthCols, strheightRows, strexitRowCoor, strexitColCoor, strenterRowCoor, strenterColCoor;

try

{

BufferedReader input = new BufferedReader(new FileReader("MazeInstructions.txt"));

try

{

line1 = input.readLine();

StringTokenizer st1 = new StringTokenizer(line1, "");

strwidthCols = st1.nextToken();

strheightRows = st1.nextToken();

}

catch (IOException z)

{

System.out.println(z);

System.exit(1);

}

}

catch (FileNotFoundException e)

{

System.out.println(e);

System.exit(1);

}

Here is what I am trying to do outside the try block:

int heightRows = Integer.parseInt(strheightRows);

((I removed some unnecessary code)

Message was edited by:

louy

louya at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 7

The compiler doesn't know that System.exit() terminates the VM. As far as the compiler knows, it's just another method call. So as far as it can tell, you could get to the heightRows = line without having set strHeightRows first.

Rather then System.exit(), you should either not catch the exception at all, or wrap it and rethrow it.

jverda at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 8
> The compiler doesn't know that System.exit() terminates the VM.Why not?Isn't that in "java.lang"?> As far as the compiler knows, it's just another method call.What?I don't understand.
ValentineSmitha at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 9

What Jeff is saying is that if something happens in either try block that throws an exception before strHeightRows is assigned a value then the compiler says it can get to the Integer.parseInt line of code where strHeightRows is unitialised because the compiler doesn't bother to check the System.exit() call will end the program.

Clear as mud?

floundera at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 10
Works for me.I wonder how I can rib him on the "just another method call" thing.
ValentineSmitha at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 11

> Rather then System.exit(), you should either not

> catch the exception at all, or wrap it and rethrow it.

From what I can tell, I have to try/catch it or the compiler spits out an error - as far as wrapping & rethrowing it, I'm going to have to do some research! But that's half the fun, right? Thanks for your help.

louya at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 12

If you want a quick hack you could try this.

int heightRows = 0;

if(strheightRows != null) {

heightRows = Integer.parseInt(strheightRows);

}

floundera at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 13

> > The compiler doesn't know that System.exit()

> terminates the VM.

>

> Why not?

> Isn't that in "java.lang"?

>

> > As far as the compiler knows, it's just another

> method call.

>

> What?

> I don't understand.

The compiler knows that throw Whatever and return cause the method (or nearest enclosing try statement) to end right there.

It knows that break and continue cause the loop to end right there.

Those are language level constructs. Special keywords.

java.lang is nothing special other than that you don't have to explicitly import it. System.exit is no different than System.getEnv as far as the compiler is concerned.

jverda at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 14

> > Rather then System.exit(), you should either not

> > catch the exception at all, or wrap it and rethrow

> it.

>

> From what I can tell, I have to try/catch it or the

> compiler spits out an error -

Not if you declare your method to throw it.

Catching it and then doing nothing just to make the compiler happy defeats the purpose of exceptions. The whole idea is that if something goes wrong that your method can't fix, the calling method knows about it.

jverda at 2007-7-15 2:45:20 > top of Java-index,Java Essentials,New To Java...
# 15
> Works for me.> I wonder how I can rib him on the "just another> method call" thing.Good luck with that. I meant what I said. :-)
jverda at 2007-7-21 12:06:59 > top of Java-index,Java Essentials,New To Java...