Java 局部变量可能尚未初始化 - 在方法中检测未检查的异常抛出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18099320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:22:59  来源:igfitidea点击:

The local variable might not have been initialized - Detect unchecked exception throw within a method

javaexceptioncompiler-errorsunchecked

提问by Luis Sep

I have some code with this structure:

我有一些具有这种结构的代码:

public void method() {
    Object o;
    try {
        o = new Object();
    } catch (Exception e) {
        //Processing, several lines
        throw new Error(); //Our own unchecked exception
    }
    doSomething(o);
}

I have quite a few methods in which I have the same code in the catch block, so I want to extract it to a method so that I can save some lines. My problem is, that if I do that, I get a compiler error " The local variable o might not have been initialized".

我有很多方法在 catch 块中有相同的代码,所以我想把它提取到一个方法中,以便我可以保存一些行。我的问题是,如果我这样做,我会收到编译器错误“局部变量 o 可能尚未初始化”。

public void method() {
    Object o;
    try {
        o = new Object();
    } catch (Exception e) {
        handleError();
    }
    //doSomething(o); compiler error
}


private void handleError() throws Error {
    //Processing, several lines
    throw new Error();
}

Is there any workaround?

有什么解决方法吗?

采纳答案by Sanjaya Liyanage

You need to initialize local variables before they are used as below

您需要在使用局部变量之前初始化它们,如下所示

public void method() {
    Object o=null;
    try {
        o = new Object();
    } catch (Exception e) {
        handleError();
    }
   doSomething(o); 
}

You will not get the compilation failure until you use local variable which was not initialized

在使用未初始化的局部变量之前,您不会遇到编译失败

回答by sanbhat

Since ois getting initialized within the tryblock and initializing omight throw an exception, java thinks that doSomething(o)statement might reach without obeing initialized. So java wants oto be initialized incase new Object()throws exception.

由于o正在try块内初始化并且初始化o可能会引发异常,因此java认为该doSomething(o)语句可能会在未o初始化的情况下到达。所以java想要o被初始化,以防new Object()抛出异常。

So initializing owith nullwill fix the issue

所以初始化onull将解决这一问题

public void method() {
    Object o = null;
    try {
        o = new Object(); //--> If new Object() throws exception then o remains uninitialized
    } catch (Exception e) {
        handleError();
    }
    if(o != null)
      doSomething(o);
}

回答by Juvanis

Initialize your object: Object o = null;, however watch out for the NullPointerExceptions that might be thrown when you give it to the method calls.

初始化您的对象:Object o = null;,但是请注意NullPointerException在将其提供给方法调用时可能会抛出的s。

回答by Gyro Gearless

Just put the doSomething(o)inside the try { }block:

只需将块放在doSomething(o)里面try { }

public void method() {
    Object o;
    try {
        o = new Object();
        doSomething(o);
    } catch (Exception e) {
        handleError();
    }

}

You perhaps dont want to execute doSomething() if the creation of your Object fails!

如果您的对象创建失败,您可能不想执行 doSomething()!

回答by Mohsin Shaikh

Instance variable is the Object type so you should initialize value "null"

实例变量是对象类型,所以你应该初始化值“null”

public void method() {
Object o=null;
try {
    o = new Object();
} catch (Exception e) {
    handleError();
}
doSomething(o); 
}