22

Generally if any class extends Exception , it becomes checked exception. Runtime exception also extends Exception. Then how is it unchecked exception?

Is it like they have a custom check in compiler for this special case?

EDIT : I have proper idea about checked v/s unchecked exception and their pros & cos etc. I don't accept differences between them in answer.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82

5 Answers5

29

It's explicitly in the specification, section 11.1.1:

RuntimeException and all its subclasses are, collectively, the runtime exception classes.

The unchecked exception classes are the runtime exception classes and the error classes.

The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.

So yes, the compiler definitely knows about RuntimeException.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • why we can't check runtime exceptions? as we know there could be a NullPointerException then we should check it. any idea? – Asif Mushtaq Mar 19 '16 at 10:59
  • 3
    @UnKnown: No, you don't want every method you ever write to have to either catch or declare NullPointerException... That would be awful. – Jon Skeet Mar 19 '16 at 11:30
  • it's mean conceptually there is no difference between checked and un-checked exceptions? just implementation makes difference? – Asif Mushtaq Mar 19 '16 at 11:33
  • 1
    @UnKnown: It depends on what you mean by "conceptually" really. – Jon Skeet Mar 19 '16 at 11:38
  • As `FileNotFoundException` checked by compiler, So it's called checked Exception, but NullPointerException is not checked by compiler its called un-checked-Exception. But both are exceptions. As you mentioned `No, you don't want every method you ever write to have to either catch or declare NullPointerException` that's why Java didn't make it as checked exception but Java can. – Asif Mushtaq Mar 19 '16 at 11:44
  • @UnKnown: Well yes, it was a deliberate decision on the part of the language designers to have both checked and unchecked exceptions... – Jon Skeet Mar 19 '16 at 11:48
11

Yes. Any Throwable is a checked exception, except for Error, RuntimeException, and (direct or indirect) subclasses thereof.

However, these are checked by the compiler, not by the JVM; checked exceptions are a compile-time feature, not a run-time feature. (Update: And I now see that you've edited your question to specify "compiler" rather than "JVM". ☺)


To elaborate a bit further . . . it's not as though there were any sort of "checked-exception" interface. The logic is simply hard-coded: "any exception class is a checked exception unless it's a subtype of RuntimeException or Error".

ruakh
  • 175,680
  • 26
  • 273
  • 307
1

Here is a useful link: http://www.javapractices.com/topic/TopicAction.do?Id=129

It explains the difference between unchecked and checked and gives some examples.

"It is somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked)."

barbiepylon
  • 891
  • 7
  • 23
0

As per 11.1.1. The Kinds of Exceptions

An exception is represented by an instance of the class Throwable (a direct subclass of Object) or one of its subclasses.

Throwable and all its subclasses are, collectively, the exception classes.

Note that a subclass of Throwable must not be generic (§8.1.2).

The classes Exception and Error are direct subclasses of Throwable.

Exception is the superclass of all the exceptions from which ordinary programs may wish to recover.

Error is the superclass of all the exceptions from which ordinary programs are not ordinarily expected to recover.

Error and all its subclasses are, collectively, the error classes.

The class Error is a separate subclass of Throwable, distinct from Exception in the class hierarchy, to allow programs to use the idiom "} catch (Exception e) {" (§11.2.3) to catch all exceptions from which recovery may be possible without catching errors from which recovery is typically not possible.

The class RuntimeException is a direct subclass of Exception. RuntimeException is the superclass of all the exceptions which may be thrown for many reasons during expression evaluation, but from which recovery may still be possible.

RuntimeException and all its subclasses are, collectively, the run-time exception classes.

The unchecked exception classes are the run-time exception classes and the error classes.

The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.

Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
0

Run-time exception is called unchecked exception since it's not checked during compile time. Everything under throwable except ERROR and RuntimeException are checked exception. Adding Runtime exception in program will decrease the clarity of program.

class Divide {
    public static void main(String [] args){
        int a = 10;
        int b = 0;
        int c = a/b; // This will throw run time exception due to unexpected value of b.
    }
}

Please read this link The Java™ Tutorials - Unchecked Exceptions — The Controversy

Adeel
  • 2,901
  • 7
  • 24
  • 34
Shamsher
  • 1
  • 3