File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Exception" Watch "Exception" New topic
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
    
    1

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
    
    4
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Exception
 
Similar Threads
helloworld error
KAM India Edition Question 6.27
Modify by throwing
Reference Specific Line in Text File
Hashtable allowing null values