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

后端 未结 5 1493
独厮守ぢ
独厮守ぢ 2020-12-18 00:40

I have some code with this structure:

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


        
5条回答
  •  抹茶落季
    2020-12-18 01:22

    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

提交回复
热议问题