8

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 { } 
9
  • More reading: stackoverflow.com/questions/3540613/… Commented Apr 10, 2014 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? Commented Apr 10, 2014 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. Commented Apr 10, 2014 at 19:06
  • 1
    Because that's how they were designed. Commented Apr 10, 2014 at 19:35
  • 1
    That's too broad to answer here. There are numerous debates about it all over the internet if you really are interested. Commented Apr 10, 2014 at 19:43

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

5 Comments

thank you, infact I was not keen on What it is, I was more onto understand the reason behind such a specification.
@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).
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.
@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.
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
2

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.

2 Comments

Yes, your understanding on my question is correct. Not only in throws clause, but also when we explicitly say throw new RunTimeException().
this answer is also correct as it clearly address my specific query.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.