Difference between Unchecked exception or runtime exception

后端 未结 8 1539
忘了有多久
忘了有多久 2020-11-29 21:55

This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the program.

8条回答
  •  旧巷少年郎
    2020-11-29 22:25

    The JavaDocs sum these up pretty well.

    java.lang.RuntimeException:

    RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

    A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

    java.lang.Error:

    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.

    Note that "unchecked exception" is merely a synonym for a RuntimeException.

提交回复
热议问题