7

How is the below one correct? I expected compiler to tell me to use throws Exception or throws RuntimeException

public void method1() throws NullPointerException { 

        throw new RuntimeException();
    }

why I think its not correct -> Bcoz a NPE is a RTE, but a RTE is not a NPE

How is this correct? I expected compiler to tell me to use throws Exception or throws RuntimeException or throws NumberFormatException

public void method2() throws NullPointerException { 


    throw new NumberFormatException();
}


public void method3() throws Exception { // this is fine, as expected

        throw new RuntimeException();
    }


    public void method4() throws RuntimeException { // this is fine, as expected

        throw new NullPointerException();
    }

    public void method5() throws Exception { // this is fine, as expected

        throw new NullPointerException(); 
    }

Answer:

for RTE even if u don't add throws clause to the method, compiler won't say anything

public void method6()  { // no compile time errors!!

        throw new NullPointerException(); 
    }

But when we explicitly say that 'throw new NullPointerException();' , why compiler ignores it? It is same as 'throw new SQLException();' It is not thrown on runtime say some object was evaluated to null, and invoked an action on that null object. Normally a function must declare all the exceptions that it can throw, but RTE's is bypassing it!

RTE's are unchecked exceptions. But when you say throw new RTE, still unchecked?!

Question - Isn't this a flaw? or please correct me in understanding why is it like that

  • Update:

Please note that this question is not about difference between checked exception and unchecked exception. The question is not about difference between any type of exception or error.

The question is why an explicitly marked RunTimeException is not handled, or left without forcing the compiler to handle it.

eg:

public void methodA() { // methodA is not forced to handle the exception.
        methodB(); 
    }

    public void methodB() throws RuntimeException {

    }
spiderman
  • 10,892
  • 12
  • 50
  • 84
  • More reading: http://stackoverflow.com/questions/3540613/please-explain-runtimeexception-in-java-and-where-it-should-be-used – Sotirios Delimanolis Apr 10 '14 at 18:47
  • Thank you, there's lot of information , and I covered only a few. But at this moment, what pops out of my head is - Why an explicitly thrown RTE is categorized as a unchecked exception in Java? – spiderman Apr 10 '14 at 19:04
  • 1
    Don't read _unchecked_ as something that the compiler doesn't see. _unchecked_ simply means that it doesn't have to be handled. – Sotirios Delimanolis Apr 10 '14 at 19:06
  • Sorry to take your time, but now my question is - Why RTE's are not handled? or need not be handled? – spiderman Apr 10 '14 at 19:34
  • 1
    Because that's how they were designed. – Sotirios Delimanolis Apr 10 '14 at 19:35
  • Do you think that is a good design? That's where my question was heading to – spiderman Apr 10 '14 at 19:39
  • 1
    That's too broad to answer here. There are numerous debates about it all over the internet if you really are interested. – Sotirios Delimanolis Apr 10 '14 at 19:43
  • Ok, that's fine. I have been working in Java over these years and never did i question it. Will go through the online resources. Thanks for your efforts and time – spiderman Apr 10 '14 at 19:47
  • for my info: I am placing a link here which is comparing java 6 and java 7 for a similar scenario: http://stackoverflow.com/questions/23041570/throw-exception-inside-catch-clause – spiderman Apr 16 '14 at 17:03

2 Answers2

12

You misunderstand.

Checked exceptions are exceptions that are checked ( hence their name ) at compile-time. Hence, if you have method doFoo that throws exception BarException, you must declare that the method throws BarException :

void doFoo() throws BarException { }

Unchecked exceptions are exceptions that are not checked by the compiler, so you do not have to declare that you throw them

Saying throw new Exception() merely throws a new instance of a checked exception, or unchecked in the case of RuntimeException. The case where the checking factors in is only when you are actually throwing a checked exception using the throw clause.

As far as whether or not it is a flaw, now that's a heavily opinionated topic. It tends to be rather annoying to use APIs that throw tons of unchecked exceptions without documenting that they throw those exceptions. However, sometimes it is possible to have exceptions that occur based on the unique runtime state of an application, where you could not declare that a certain checked exception could be thrown, and that is where runtime exceptions shine (like NullPointerException)

TTT
  • 1,952
  • 18
  • 33
  • thank you, infact I was not keen on What it is, I was more onto understand the reason behind such a specification. – spiderman Apr 10 '14 at 19:41
  • @prash the reason for needing unchecked exceptions... They are defined as exceptions that occur during runtime. I can't give you a definite engineering decision(only Oracle engineers can) that was behind it... But I would suspect that it is because certain exceptions can only occur during runtime, like nullpointers, so unchecked exceptions are completely necessary. Checked, however, were likely a design decision to protect developers from silly exception-related issues... Java offers compiler enforcement of other things too (like the `@overload` tag). – TTT Apr 10 '14 at 20:15
  • Thank you for those details. Infact I agree that we need unchecked exceptions, eg NullPointerException, ArithmeticException etc occurs at runtime, during the execution. My question is when we explicity throw a new RuntimeException, say by `throw new RunTimeException()` or `throws RunTimeException` the scenario is different. This is somthing similar as you write `throw new SQLException`. – spiderman Apr 11 '14 at 01:46
  • @prash so you're wondering why if you explicitly declare unchecked exceptions with `throws`, the java compiler does not check those exceptions are caught? well, yes, it would be cool and very useful if we could turn unchecked exceptions into checked exceptions just by adding them to a `throws` clause. The answer to your question is that the java compiler _unconditionally_ does not check unchecked exceptions. Adding unchecked exceptions to a throw clause doesn't actually do anything on a compiler-level, it is good practice when you know that certain RuntimeExceptions are to be expected, though. – TTT Apr 11 '14 at 14:29
  • Looking at my question today... Was I so dump that time not to realize how the hell could I expect COMPILER to give hint on UNCHECKED exception? my bad !! lol – spiderman Oct 31 '15 at 20:30
1

I think I understand the question, you want to know if it's a design flaw that whatever exception you include in a method's throws clause doesn't get counted as checked.

The core premise of checked exceptions was that we could tell, at the site where an exception was thrown, whether anyone would want to handle this exception or not. There wasn't any consideration if a NullPointerException should be considered as checked in some contexts but not in others, the idea was that it was always one thing or another. Also for this to be included initially this would have been more code to write at a time when there were tight deadlines, and after that a change to this would've meant breaking existing user code. So that's likely why Java doesn't have that feature.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Yes, your understanding on my question is correct. Not only in throws clause, but also when we explicitly say `throw new RunTimeException()`. – spiderman Apr 11 '14 at 01:48
  • this answer is also correct as it clearly address my specific query. – spiderman Apr 16 '14 at 16:57