why does the catch block give an error with variable not initialized in Java

后端 未结 7 1004
臣服心动
臣服心动 2021-01-12 00:42

This is the code that I\'ve written.

int num;
try {
    num=100;
    DoSomething();
    System.out.println(num);
} catch(Exception e) {
    DoSomething1();
}         


        
7条回答
  •  攒了一身酷
    2021-01-12 00:52

    You have putten num = 100 inside yout try block, the compiler assumes that an error may occur before num = 100 is reached. Thus, when you enter in the Catch bloch, for the compiler, he only sees int num which gives you the Variable not initialized error.

提交回复
热议问题