开发者

Java: how to access assignments in try-catch -loop?

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.u开发者_JAVA技巧til.*;
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);
 }
}


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.


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);   


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜