1

Isn't it very confusing that the RuntimeException (which is a base class of all unchecked exception) inherits from Exception class Which is a base class for checked exceptions.

Question- So how does it work?

My Understanding - The Idea - Exception class will have a method say boolean toCheck() which would return true or false to tell the compiler to check for the checked exceptions or not.

And in the RuntimeException extends Exception , we simply override the boolean toCheck() method to always return false. This will the tell the compiler to not to check for exceptions before hand.

Is it how it works?

Also why didn't the designers make a new class to inherit directly from throwable?

lynxx
  • 544
  • 3
  • 18

3 Answers3

3

The specification makes that very clear, in §11.1.1. The Kinds of Exceptions:

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

So this is indeed "burned" into the compiler - meaning it does know about RuntimeException specifically.

Community
  • 1
  • 1
Eugene
  • 117,005
  • 15
  • 201
  • 306
3

Your understanding is not correct Runtime exception is not designed as the base class for unchecked exception

From the oracle doc

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

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.

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.

The checked exception classes are all exception classes other than the unchecked exception classes.

Community
  • 1
  • 1
Ravi
  • 719
  • 8
  • 23
  • @lynxx Please accept if it answers your question – Ravi Mar 27 '19 at 09:52
  • How does the compiler know wheather to check for required exceptions or not to check? – lynxx Mar 27 '19 at 11:03
  • 2
    Checked Exception is subclass of java.lang.Exception. Hence, all Exception present in Exception class is treated as checked exception by compiler except those present in java.lang.RuntimeException. – Ravi Mar 27 '19 at 16:02
  • 2
    Unchecked exception has wider range and all exception which does not lie in unchecked is treated as checked by the compiler – Ravi Mar 27 '19 at 16:05
  • @Iynxx Please accept the answer – Ravi Apr 03 '19 at 11:13
1

Let us look at the definition of Exception from docs.oracle.com.

" The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch."

" The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions."

So it is never told that Exception is a base class only for checked exceptions. It is the base class of both the exceptions. All Checked Exceptions and also Runtime Exceptions.

As per the property of Inheritance the Subclass will inherit the methods and variables of the Parent class applied the restrictions of access specifiers and it will also have its own properties.

Samik
  • 29
  • 2
  • 12
  • 1
    How does the compiler know for wheather to check if they are throwing the required exception at compile time (checked exceptions) and for which not to check (unchecked)? – lynxx Mar 27 '19 at 11:00
  • 2
    See if you are using a class inside your code which itself or any method of that class is throwing a checked exception (There are defined checked Exception classes in Java) then your code must handle that exception either by try-catch or mentioning throws. And the unchecked exception are those which will not cause the compiler to stop the program from executing. It let the code to compile and when the Unchecked exception is faced then throws it. All unchecked exceptions base class is Runtime Exception so the compiler will never check anywhere else than RuntimeException and its subclasses. – Samik Mar 27 '19 at 11:22
  • 2
    Please Read These two links: https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/ https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html – Samik Mar 27 '19 at 11:23