| Author |
Exception
|
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
Hello
It is valid to throw RuntimeException then why doesn't it compile ?
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
|
Whats the compilation error? Can you draw some conclusion from it why its not allowed to explicitly throw runtime exception from _initialization_ block.
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
Null.java:3: error: initializer must be able to complete normally
static{
^
1 error
I know this that we can throw unchecked exception in static initilizer block to get an ExceptionInInitializerError but not checked exceptions,so what's wrong here ?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
It seems the compiler is smart enough to see that if that class is initialized, it will always cause an error. Why would you want such a class anyway? Once you try to load it your JVM will exit.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
Rob Spoor wrote:It seems the compiler is smart enough to see that if that class is initialized, it will always cause an error. Why would you want such a class anyway? Once you try to load it your JVM will exit.
why does it work ? it is also an explicit exception declaration for compiler.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
Because the compiler is not smart enough to see that an if (true) will always have its body executed. If you search this forum there are some discussions about that.
|
 |
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
Rob Spoor wrote:It seems the compiler is smart enough to see that if that class is initialized, it will always cause an error. Why would you want such a class anyway? Once you try to load it your JVM will exit.
Rob Spoor wrote:Because the compiler is not smart enough to see that an if (true) will always have its body executed. If you search this forum there are some discussions about that.
Sometimes you say compiler is enough smart and sometimes you say compiler is not enough smart lol.I am not kid.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32831
|
|
|
Before you call compilers smart or not smart, you ought to try writing one. Picking up that sort of path through the code is difficult. So there are some things which a compiler cannot readily be programmed to handle.
|
 |
 |
|
|
subject: Exception
|
|
|