0

I got curious about the 'throws' clause and wrote the following piece of code (I use Eclipse with Java7). Originally I had started with only blocks 1 and 5 (expecting a compilation error, which did not occur...) and then this led me to writing the other blocks.

// 1
public void throwNPE() {
    throw new NullPointerException();
}

// 2
public void throwNPEWithGenericClause() throws Exception {
    throw new NullPointerException();
}

// 3
public void throwNPEWithNPEClause() throws NullPointerException {
    throw new NullPointerException();
}

// 4
public void throwNPEWithIAEClause() throws IllegalArgumentException {
    throw new NullPointerException();
}

// 5
public void callThrowNPE() {
    throwNPE();
}

// 6
public void callThrowNPEWithGenericClause() {
    throwNPEWithGenericClause(); // COMPILATION ERROR
}

// 7
public void callThrowNPEWithNPEClause() {
    throwNPEWithNPEClause();
}

// 8
public void callThrowNPEWithIAEClause() {
    throwNPEWithIAEClause();
}

To be honest I would have expected:

(a) a compilation error in 1. (unhandled exception? shouldn't my method notify any 'subsequent caller' that this is going to throw some kind of exception?)

(b) some kind of problem in 4. (possibly a compilation error? I'm throwing a NPE while the clause says IAE)

(c) compilation errors in 5. 6. 7. and 8. (unhandled exceptions? I'm omitting the 'throws' clause)

(d) perhaps someone could also tell me why 6. is the only one where I got the compilation error...

PLB
  • 881
  • 7
  • 20
  • 3
    Learn about Unhecked Exceptions. – SLaks Mar 12 '15 at 20:54
  • 1
    You don't have to catch and handle RuntimeExceptions like `NullPointerException` or `IllegalArgumentException`. – Pshemo Mar 12 '15 at 20:55
  • 4
    See [Java: checked vs unchecked exception explanation](http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – Alex Mar 12 '15 at 20:56
  • oh that's a game changer. thanks I will have a look at it! – PLB Mar 12 '15 at 20:59

3 Answers3

3

The Exceptions in Java can be divided in three different Base-types:

Error (thrown by the JVM, if a fatal Error occoures)

Exception (all checked Exceptions inherits from this one)

RuntimeException (all unchecked Exceptions inherits from this)

All are Throwables

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • @christian-kuetback is right. In addition, "throws" doesn't mean that Exception should be inside a try catch, the exception type means! – Danilo Gomes Mar 12 '15 at 21:25
1

RuntimeException or Unchecked Exception instances like NPE do not require throwing/catching. They can be caught, but generally you don't want to because it indicates an abnormal program flow and it should terminate the program. In general, if there is no possibility to proceed then it will terminate as a RuntimeException. If you want to prevent some kind of action on a null value then you should check if it is null rather than expecting an NPE.

tinker
  • 1,396
  • 11
  • 20
1

RuntimeException is unchecked, so compiler does not warn when you throw a exceptions subclass of RuntimeException. If you need compiler to warn you, then you should use Exception or its subclasses.

1) NullPointerException extends RuntimeException so compiler does not give any error.

2) Even though your method throws NullPointerException, since you marked the method with throws Exception, compiler warns you to catch it in it's callers.

3) Same as 1st answer

4) Same as 1st answer IllegalArgumentException extends RuntimeException

5) throwNPE does not throw anything at all.

6) Eventhough you throw a NullPointerException (RuntimeException) within throwNPEWithGenericClause, since you mark the method as checked exception, compiler does not allow.

7, 8) Same as 1st answer. Both runtime exceptions, no need to check.

Onur Aktaş
  • 410
  • 4
  • 8