• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable scope and initializing variables  RSS feed

 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I declare a variable inside a catch try block, that variable is not available outside of the block. Example:



On the other hand, if I declare the variable outside the try block, I'm told the variable might not be initialized. Example:



I suppose I could initialize the variable to a temporary value that it's never really supposed to have, but that seems risky so I'm guessing there's a better way. Thoughts?
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
26.Play.java:16: error: variable y might not have been initialized


...this error is because if exception is thrown then y remains unassigned...so may be you can assign some
'appropriate' value to y in check block.

OR

may be use first approach and print y in try itself and SOP appropriate error message in check block.

Manish
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Wallace wrote:I suppose I could initialize the variable to a temporary value that it's never really supposed to have, but that seems risky so I'm guessing there's a better way. Thoughts?

Class variables are initialized to default values automatically. Method variables are not. Thats why the compiler is asking you to initialize it.
By the way, the default for an int is 0 not 1.
Jonathan Wallace wrote:
Play.java:14: error: cannot find symbol
System.out.println ( y );

Thats because the scope of 'y' is limited to the try block and as such it is not "visible" outside it
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:
...this error is because if exception is thrown then y remains unassigned...

No. It has got nothing to do with exception or try catch. You will still get the same compiler error without it. Please read my post above .
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is, what do you want to actually happen if an exception is thrown? You say you think it's risky to provide a temporary value - but in that case surely it's risky to initialise it to a temporary value as well?

If you want a particular value of y to be printed if an exception is thrown, set it to that value. If you don't want anything to be printed, put the print statement inside the try/catch block so that it's skipped entirely.


Oh, and I know this is just test code, but I'd recommend getting into the habit of never writing catch(Exception) {}. The catch block should never be empty, because that way you've got absolutely no idea what happened if it goes wrong. Print or log the stack trace at least. I've seen plenty of questions in the forum from people asking why their code doesn't work, when the computer is telling them why it doesn't work and they've chosen to ignore it! In the very few occasions where you do need to entirely ignore an exception, put a comment in the block stating this.
 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:The question is, what do you want to actually happen if an exception is thrown? You say you think it's risky to provide a temporary value - but in that case surely it's risky to initialise it to a temporary value as well?


Hmm you're right, the logic is flawed. However, putting that aside for the moment, I suppose the question I'm getting as it how I can use the 'y' value returned by riskyMethod within the main method, but outside the catch try block. 'Best practice'?

Oh, and I know this is just test code, but I'd recommend getting into the habit of never writing catch(Exception) {}. The catch block should never be empty, because that way you've got absolutely no idea what happened if it goes wrong. Print or log the stack trace at least. I've seen plenty of questions in the forum from people asking why their code doesn't work, when the computer is telling them why it doesn't work and they've chosen to ignore it! In the very few occasions where you do need to entirely ignore an exception, put a comment in the block stating this.


I had no idea you could do that.... Quick google: seems you would print "ex.getMessage()". Is that what you meant?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Wallace wrote:Hmm you're right, the logic is flawed. However, putting that aside for the moment, I suppose the question I'm getting as it how I can use the 'y' value returned by riskyMethod within the main method, but outside the catch try block. 'Best practice'?

What you're doing is fine, and if riskyMethod doesn't throw an exception, then its return value is normally assigned to 'y', so you can use it after the catch block as you like. The point is that 'y' must in all possible scenarios get assigned a value before you can use it - whether riskyMethod throws an exception or not. The only way is to declare the variable 'y' outside the catch block, and either assign it some default value at the line where you're declaring it, or assigning it some appropriate value inside the catch block.

Jonathan Wallace wrote:Quick google: seems you would print "ex.getMessage()". Is that what you meant?

You can call ex.printStackTrace(); inside the catch block to print the exception message and stack trace.

Example:

Or:
 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allright, I think I've got it. Thank you!
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
manish ghildiyal wrote:
...this error is because if exception is thrown then y remains unassigned...

No. It has got nothing to do with exception or try catch. You will still get the same compiler error without it. Please read my post above .




I removed try/catch blocks and got no compiler error.Program ran with output 1, just as was expected.

Manish
 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:

I removed try/catch blocks and got no compiler error.Program ran with output 1, just as was expected.

Manish


The problem is about catch try though. That is, how do you use a return value recieved within the try block, in the rest of the function.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:
I removed try/catch blocks and got no compiler error.Program ran with output 1, just as was expected.
Manish


My bad. I missed out on the part where y is being assigned a value. I stand corrected.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!