简体   繁体   English

try catch语句的范围问题

[英]Scope issue on a try catch statement

I have a n00b/basic issue about the try catch on java. 我有一个关于java的try catch的n00b / basic问题。

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

myIni.get("toto");

and the error message that follows : variable myIni might not have been initialized 以及后面的错误消息: 变量myIni可能尚未初始化

Is the scope of the try only restricted to the try area ? 尝试的范围仅限于尝试区域吗? How can I fetch the result of myIni in the following code ? 如何在以下代码中获取myIni的结果?

To avoid the message, you need to set a default value before the try statement. 要避免该消息,您需要在try语句之前设置默认值。
Or you need to put the call to the get() method in the try statement. 或者您需要将调用放在try语句中的get()方法中。

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"); 要解决您的问题, myIni使用null初始化myIni ,并注意如果尝试失败,请myIni.get("toto"); would result in a NullPointerException. 会导致NullPointerException。

So you'd need to either account for that or throw another exception from your catch block. 所以你需要考虑到这个或者从catch块中抛出另一个异常。

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. 正如已经@khachik你也可以把try块周围的整个使用建议myIni如果可能的话,适当。 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"); 以你正确的方式做你想要的是把myIni.get("toto"); inside the try block: try块内:

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

Don't do Ini myIni = null; 不要做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 . 在这种情况下,如果在myIni初始化时抛出IOException ,则代码将抛出NullPointerException

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. 为了避免程序收到NullPointerException ,执行检查以确保try块中的被调用导致Object已经构建了一些数据。

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 / catch块之外调用myIn,则可以选择,因为如果发生异常,该对象实际上将为null ,那么您可以执行以下操作。

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

This is the way of the compiler to say that the initialization of myIni might fail. 这是编译器说myIni初始化可能失败的方式。 Because the myIni = new Ini(new FileReader(myFile)); 因为myIni = new Ini(new FileReader(myFile)); line might throw an exception. line可能会抛出异常。

If it fails when you get to the line myIni.get("toto"); 如果它到达myIni.get("toto");失败myIni.get("toto"); myIni would not have been initialized. myIni不会被初始化。

You have 2 choices: 你有2个选择:

  1. Put the myIni.get("toto"); myIni.get("toto"); inside the try block. try块内。
  2. Assign an initial null value to myIni when you define it and check for null outside of the try block. 在定义它时为myIni分配初始null值,并在try块之外检查null

Just put myIni.get("toto") inside the try catch block, or write Ini myIni = null; 只需将myIni.get(“toto”)放在try catch块中,或者写入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... 请注意,如果您执行第二个变体,如果找不到该文件,或者由于任何其他原因无法读取,则可能会出现NullPointerException ...

cheers! 干杯! P P

write Ini myIni = null; Ini myIni = null; and that's it 就是这样

In the catch statement, you don't define the value of the variable. 在catch语句中,您不定义变量的值。 Therefore, the variable won't have a value if you catch then run myIni.get("toto"); 因此,如果你捕获然后运行myIni.get("toto"); ,变量将没有值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() . 即便如此,运行get()时也会获得NPE。

in your code, if an exception happens, the variable myIni could not be instantiated, that's why the compiler rises such a warning. 在您的代码中,如果发生异常,则无法实例化变量myIni,这就是编译器发出此类警告的原因。

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. 正如@khachik指出的那样,最好在try块本身内声明并初始化变量。 Initialize like below, outside the try bock only if you feel supremely confident which is indistinguishable from arrogance. 只有当你感到极度自信而且与傲慢无法区分时,才会在下面进行初始化。

Ini myIni = null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM