I didn't understand the difference between these codes. One of them is compiled, the other one isn't.
{
if (true) {
try {
throw new IOException();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
This is a instance initializer, i throw a checked exception and then handle it, and this code compile.
But this one isn't compile.
{
while (true) {
try {
throw new IOException();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Exception says: Initializer must be able to complete normally
Here is the image of both Instance Initializer
if (true), therefore it cannot know if the first one actually throws an exception. The second one either never ends or always throws an exception. The compiler can understand the while is entered but never properly exited. The try-catch is irrelevant in both cases, just throw a new and empty exception in both cases.while(true)andif(true)in this case