Java RuntimeExceptions and Errors

According to Sams Teach Yourself Java in 21 Days

"Unchecked exceptions, also called runtime exceptions ..."

Under this fact, an error is also an exception at runtime, as they are unchecked exceptions (or is this not what it says?)

This confuses me with the statement below.

Runtime exceptions are internal errors at runtime in the Java environment.

If he speaks of java RuntimeExceptions, then it is a lie because they are Exceptions, which are described as "An exception describes errors caused by your program and external circumstances. These errors can be caught and processed by your program."

But, on the other hand, if this refers to java errors, then these are internal errors.

So is this statement true or false?

+4
source share
3 answers

The best place for such an explanation is the official documentation. Throwable- a superclass in which there is Errorand Exception. RuntimeExceptionis a subclass of under Exception.

  • Error, and its subclasses are not marked.
  • Exception and its subclasses are checked;
    • except for branching RuntimeException.

The difference between classes Errorand RuntimeExceptionis in the fact that

  • Errornot under your control. This is usually a problem with the system / environment; for example OutOfMemoryError.
  • RuntimeException, on the other hand, represent a flaw in the logic of your program, that is, in your control. You can fix it. For instance.NullPointerException
+6

, . RuntimeException , , , .

+1

To exclude Runtime, a try-catch block is not required, and a constructor or method does not need a statement throws.

the doc page from oracle makes some useful explanations.

0
source

Source: https://habr.com/ru/post/1598593/


All Articles