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
The local variable might not have been initialized - Detect unchecked exception throw within a method
提问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 o
is getting initialized within the try
block and initializing o
might throw an exception, java thinks that doSomething(o)
statement might reach without o
being initialized. So java wants o
to be initialized incase new Object()
throws exception.
由于o
正在try
块内初始化并且初始化o
可能会引发异常,因此java认为该doSomething(o)
语句可能会在未o
初始化的情况下到达。所以java想要o
被初始化,以防new Object()
抛出异常。
So initializing o
with null
will fix the issue
所以初始化o
与null
将解决这一问题
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 NullPointerException
s 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);
}