This is the code that I\'ve written.
int num;
try {
num=100;
DoSomething();
System.out.println(num);
} catch(Exception e) {
DoSomething1();
}
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.
In Java you must init local vars. This is not guaranteed when you've these catch blocks since an exception may be thrown before init the local var in try block and you do not init the var in the catch block. Therefore the local var may not have been initialized after try-catch-finally block.
When you remove the catch block then the var is initialized after try or an exception is thrown and the code after try-block never get executed.
You've following possibilities to fix this:
The local var is not guaranteed to be initialized even when var init is the first thing you do in try-block. For example the executed thread can be stopped with Thread.stop(Throwable)
after entering try-block but before var init. When this happens then the var is not initialized but catch-finally is executed and the code after try-catch-finally as well.
Here's an example to show what I mean:
public static void main(String[] args) throws InterruptedException {
Thread thread = new MyThread();
thread.start();
Thread.sleep(100);
thread.stop(new Exception());
}
private static class MyThread extends Thread {
@Override
public void run() {
int num = -1;
try {
Thread.sleep(600); //just to hit the thread at the right point
num = 100;
System.out.println("blub");
}
catch (Exception e) {
System.out.println("catch");
}
finally {
System.out.println("finally");
}
System.out.println(num);
System.out.println("after all");
}
}
This code prints out:
catch
finally
-1
after all
Exception handling can confuse the compiler. In this case, you know that an exception cannot possibly be thrown BEFORE the variable num
is set, but the compiler doesn't know this.
The compiler thinks that an exception might be thrown before num
has been set in which case num
would not have been initialized when you try to print it outside the try-catch block.
I happened to have blogged about this yesterday http://vanillajava.blogspot.com/2012/01/odd-case-of-initialization.html
In short, the compiler doesn't know if the variable has been initialized inside a try/catch, even in the most trivial case. For this reason, it complains the variable might not have been initialised.
Interestingly, if you make the variable final and try to set it after the try/catch block the compiler will complain it might have been initialized.
If there is an exception thrown in your try
block, then the variable num
may indeed not have been initialised. If you include the catch
block, then execution can continue to the error line regardless, and thus the compiler reports the error you state.
If you remove the catch
block, then execution will only reach the "error line" if there has been no exception, and in this case, the variable will have been initialised within the try
.
(I'm assuming you already know about the need to intialise local variables before using them, and have focused on the behaviour you noticed with the catch
block...)
Local variables in Java are not initialized automatically. So you need to initialize it before use them.
int num=0;
try
{
..
}
...
Fore more info read - Definite Assignment (JLS - 16.2.15 try Statements)