开发者

Scope issue on a try catch statement

I have a n00b/basic issue about the try catch on java.

Ini myIni;
try {
    myIni = new Ini(new FileReader(myFile));
} catch开发者_如何学Go (IOException e) {
    e.printStackTrace();
}

myIni.get("toto");

and the error message that follows : variable myIni might not have been initialized

Is the scope of the try only restricted to the try area ? How can I fetch the result of myIni in the following code ?


To avoid the message, you need to set a default value before the try statement.
Or you need to put the call to the get() method in the try statement.


Yes, the scope of the try is restricted to that. In fact the scope starts with { and ends with }, thus this would also create a sub-scope

void foo() {
  {
    Ini  myIni = new Ini(new FileReader(myFile));
  }

  myIni.get("toto"); //error here, since myIni is out of scope
}

To fix your issue, initialize myIni with null, and be aware that if the try fails, myIni.get("toto"); would result in a NullPointerException.

So you'd need to either account for that or throw another exception from your catch block.

Check for null:

 Ini myIni = null;
 try {
   myIni = new Ini(new FileReader(myFile));
 } catch (IOException e) {
   e.printStackTrace();
 }

 if( myIni != null ) {
   myIni.get("toto");
   //access the rest of myIni
 } else {
   //handle initialization error
 }

Throw exception:

 Ini myIni = null;
 try {
   myIni = new Ini(new FileReader(myFile));
 } catch (IOException e) {
   e.printStackTrace();
   throw new MyCustomInitFailedException(); //throw any exception that might be appropriate, possibly wrapping e
 }

 myIni.get("toto");

As already suggested by @khachik you could also put the try block around your entire usage of myIni if that's possible and appropriate. What solution you choose depends on your other requirements and your design.


In your the correct way to do what you want is to put myIni.get("toto"); inside the try block:

try {
    Ini myIni = new Ini(new FileReader(myFile));
    myIni.get("toto");
} catch (IOException e) {
    e.printStackTrace();
}

Don't do Ini myIni = null; as some answers suggested. In this case, your code will throw NullPointerException, if an IOException is thrown on the initialization of myIni.


This is the way of the compiler to say that the initialization of myIni might fail. Because the myIni = new Ini(new FileReader(myFile)); line might throw an exception.

If it fails when you get to the line myIni.get("toto"); myIni would not have been initialized.

You have 2 choices:

  1. Put the myIni.get("toto"); inside the try block.
  2. Assign an initial null value to myIni when you define it and check for null outside of the try block.


Is the scope of the try only restricted to the try area ? The answer is yes. The issue you having is you've forgotting to initialize your object.

Try this:

Ini myIni=null;
try {
    myIni = new Ini(new FileReader(myFile));
} catch (IOException e) {
    e.printStackTrace();
}

To avoid your program from recieving NullPointerException, performing a check to make sure the called within the try block resulted in the Object been build with some data.

if(myIni !=null) 
 {
   myIni.get("toto");
 }

Alternative if you do not want to call myIni outside the try/catch block because if an exception occurs, the object will in effect be null, then you can do as follows.

try {
    Ini myIni= new Ini(new FileReader(myFile));
    myIni.get("toto");
} catch (IOException e) {
    e.printStackTrace();
}


Just put myIni.get("toto") inside the try catch block, or write Ini myIni = null; in the first row. Note that if you do the second variant you might get a NullPointerException if the file is not found, or cannot be read for any other reason...

cheers! P


write Ini myIni = null; and that's it


In the catch statement, you don't define the value of the variable. Therefore, the variable won't have a value if you catch then run myIni.get("toto");. You'd want to do something like this:

Ini myIni = null;
try {
    myIni = new Ini(new FileReader(myFile));
} catch (IOException e) {
    e.printStackTrace();
}

myIni.get("toto");

Even so, you'll get an NPE when running get().


in your code, if an exception happens, the variable myIni could not be instantiated, that's why the compiler rises such a warning.

you can change it to:

Ini myIni = null;
try {
    myIni = new Ini(new FileReader(myFile));
} catch (IOException e) {
    e.printStackTrace();
}
if(myIni!=null){
myIni.get("toto");
}


As @khachik pointed out, it is best to declare and initialize the variable inside the try block itself. Initialize like below, outside the try bock only if you feel supremely confident which is indistinguishable from arrogance.

Ini myIni = null;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜