-1

I had a discussion with one of the seniors on Exception in Java on concept
If I create a class by extending Exception class then it will be checked exception
If I create a class by extending Throwable,RuntimeException, or Error, then it will be unchecked Exception

Is the above statements correct ?
Note: I have through this link When to choose checked and unchecked exceptions

EDIT:
While going through the answer @Daemon, he says
'Throwable on the other hand, is extended by Exception, and is therefore a checked exception.'
I have a question : Throwable is extended by Error also, so why not it is Unchecked ?

Community
  • 1
  • 1
lowLatency
  • 5,534
  • 12
  • 44
  • 70
  • Throwable is to Exception/Error classes what Object is to all Java classes. It defines those basic methods and attributes that all Java exceptions should have (by default). Error's relationship to Throwable is similar to RuntimeException's relationship to Exception. In the simplest terms, Errors are unchecked Throwables, and RuntimeExceptions are Unchecked Exceptions. The rule is: all Throwables are Checked unless otherwise specified (this is the case in Error and RuntimeException). I'll edit my answer with a breakdown of each of these classes. – Taylor Hx Aug 08 '13 at 02:31
  • To answer your question, Error is unchecked because they are written to be unchecked. A reasonable program should _not_ try to catch Errors, so why force them to check for them? – Taylor Hx Aug 08 '13 at 02:50

5 Answers5

2

Anything that extends Exception but does not extend RuntimeException. Error and Throwable should not be used by Java programs, as Error is intended to communicate serious, unrecoverable problems such as completely running out of memory, hardware problems, or the like.

Technically, only classes that extend Exception are exceptions; Errors and other Throwables are not exceptions and are not expected to be caught by regular programs.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
2

You are correct that any class extending Exception will be a checked exception, however you are mistaken with your next statement:

If I create a class by extending Throwable,RuntimeException, or Error, then it will be unchecked Exception

If you extend RuntimeException or Error, your class will be a unchecked exception.

Throwable on the other hand, is extended by Exception, and like Exception is a checked exception.

The below code demonstrates these practically. For more information, Barry Ruzek's "Effective Java Exceptions" makes for some very interesting reading on the subject.

public class JavaExceptions {

    public static void throwException() throws Exception {
        throw new Exception("This is a Checked Exception.");
    }

    public static void throwRuntimeException() throws RuntimeException {
        throw new RuntimeException("This is a Runtime Exception.");
    }

    public static void throwError() throws Error {
        throw new Error("This is an Error.");
    }

    public static void throwThrowable() throws Throwable {
        throw new Throwable("This is a Throwable.");
    }

    public static void main (String... args) {

        //Exception extends Throwable, thus both are checked Exceptions.
        try {
            throwThrowable();
            throwException();
        } catch (Throwable e) {
            //Handle exception (or throwable in this case)...
            e.printStackTrace();
        }

        //RuntimeException and Error are both unchecked exceptions.
        throwRuntimeException();
        throwError();
    }
}

EDIT 1: In regards to Error and its use, this is taken from Oracle's Documentation:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it. A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

EDIT 2: The classes we are dealing with here can be described as follows:

Throwable extends Object: Top-level class that should be extended by any class that needs to be thrown (like Exceptions and Errors). Throwables are checked. See: Throwable (Java Platform SE 7)

Error extends Throwable: A subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Overides Throwable functionality to become unchecked. See: Error (Java Platform SE 7)

Exception extends Throwable: Another subclass of Throwable that indicate conditions that a reasonable application might want to catch. Like Throwable, is checked. See: Exception (Java Platform SE 7)

RuntimeException extends Exception: The superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. Overrides Exception functionality to become unchecked. See: RuntimeException (Java Platform SE 7)

Taylor Hx
  • 2,815
  • 23
  • 36
  • Thanks for the inputs with the help of example, but as you say 'Throwable on the other hand, is extended by Exception, and is therefore a checked exception.', I have a question : Throwable is extended by Error also, so why not it is Unchecked ? – lowLatency Aug 05 '13 at 06:10
  • That is an excellent question. Error extends Throwable as Errors are Throwable, however Errors should rarely be thrown by Java applications but by the JVM in abnormal conditions that should never occur. Therefore, Errors should not be thought of as unchecked Exceptions but rather the product of irrecoverable failures unrelated to Exceptions. – Taylor Hx Aug 05 '13 at 06:21
1

From the oracle documentation: "For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions. " So, your first statement is correct, the second is not. You can read more in the SCJP Sun Certified Programmer for Java 6 pg. 356-380.

Dan94
  • 346
  • 1
  • 3
0

If I create a class by extending Exception class then it will be checked exception

This is Correct.

If I create a class by extending Throwable,RuntimeException, or Error, then it will be unchecked Exception

This is incorrect. The class RuntimeException and its subclasses, and the class Error and its subclasses are unchecked exceptions classes. Because the compiler doesn’t forces them to be declared in the throws clause. All the other exception classes that are part of Throwable hierarchy are checked exceptions.

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
0

Exception class and Error class both extends Throwable class. RuntimeException class extends Exception class. Checked exceptions should be handles using try catch or throws in compile time. No need for that handling for RunTimeExceptions. Errors should not be handled but let them occur cause they are serious things.

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50