views:

205

answers:

5

The problem drives me to the big try-catch loops. I want smaller. So how to access assignments in the loops?

$ javac TestInit2.java 
TestInit2.java:13: variable unknown might not have been initialized
  System.out.println(unknown);
                     ^
1 error

Code

import java.util.*;
import java.io.*;

public class TestInit2 {

 public static void main(String[] args){
  String unknown;
  try{
   unknown="cannot see me, why?";
  }catch(Exception e){
   e.printStackTrace();
  }
  System.out.println(unknown);
 }
}
+7  A: 

You need to assign an initial value to unknown (in this case I have used null but feel free to assign any value that makes sense for your application):

import java.util.*;
import java.io.*;

public class TestInit2 {

    public static void main(String[] args){
        String unknown = null;
        try{
            unknown="cannot see me, why?";
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println(unknown);
    }
}

Since your assignment occurs inside a try the compiler has no way of verifying that unknown will ever be assigned a value so it is possible that you could use the variable without an assigned value.

Andrew Hare
I think I'd start with a default value of "(an error occurred)" or something like that.
extraneon
Throwing an exception from inside the catch - i.e. if the exception is not recoverable - or setting the variable value in the catch clause would solve it as well.
aperkins
+2  A: 

You never give unknown a value when you declare it, not even null. The compiler doesn't know what happens in the try block, and can't guarantee that anything inside takes place. So, as far as it knows, unknown isn't referencing anything until the moment your println() hits.

EDIT: To clarify, all the compiler knows is that an exception might be thrown at any point during a try block, so it can't assume that what you put in there will succeed.

Lord Torgamus
main point: you need to initialize value outside try-catch?
HH
Right, you should initialize values outside of try-catch blocks, even if it's just with dummy values like null. But really, the take-away here is that you can't absolutely rely on anything in a try or catch to happen.
Lord Torgamus
+2  A: 

You are "trying" to set that variable's value, which wasn't initialized. That means that if an exception was caught when trying to do that assignment, the assignment wouldn't have happened and the variable would remain uninitialized. To get rid of the error, declare the variable like:

String unknown = null;

That way, at the very least, println will be able to resolve something.

javanix
+3  A: 

The compiler is stopping you from doing something that is most likely a mistake, since after your try-catch block, you would probably assume that the variable is initialized. If an exception is thrown, however, it will not be initialized.

You will need to assign the variable to something before using it. It is, however, possible to just assign it to null, if you want it to be null if the assignment fails.

So, if you want the variable to be null if the assignment fails, try this:

    String unknown = null;
    try{
        unknown="cannot see me, why?";
    }catch(Exception e){
        e.printStackTrace();
    }
    System.out.println(unknown);

If you want to set the variable to something else if an exception is caught, try this:

    String unknown;
    try{
        unknown="cannot see me, why?";
    }catch(Exception e){
        e.printStackTrace();
        unknown = "exception caught";
    }
    System.out.println(unknown);        

Also, if it doesn't make sense to proceed with the execution of your method if the assignment fails, you might want to consider either returning from the catch block, or throwing another exception which would be caught by the caller. For example:

    String unknown;
    try{
        unknown="cannot see me, why?";
    }catch(Exception e){
        e.printStackTrace();
        //return; // if you just want to give up with this method, but not bother breaking the flow of the caller
        throw new Exception("Uh-oh...", e); // if you want to be sure the caller knows something went wrong
    }
    System.out.println(unknown);   
pkaeding
When would you throw Exception inside try-loop, not catch?
HH
you generally don't throw an exception directly from inside a try block that is caught by the corresponding catch block, usually some call you make inside a try block might throw an exception.Rethrowing an exception from a catch block can be useful, though, if you want to be sure that whoever called your method is aware that it did not succeed. You can do something with the exception first, such as log it, and then either rethrow it directly, or wrap it in a new exception, and throw that one.
pkaeding
+1  A: 

The key word in the compiler message is might. It doesn't mean it's not possible for the variable to be initialized when the variable is accessed in the println call, just that there is a possibility that it would not be, hence the compiler complains.

JRL