Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java RuntimeExceptions and Errors

According to "Sams Teach Yourself Java in 21 Days" book

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

Under that fact,Error are also runtime exception as they are unchecked exceptions( or is it not what it says?)

This confuses me with the below statement.

Run Time Exceptions are internal errors in the Java run time environment.

If it talks about java RuntimeExceptions then,it's false because they are Exceptions which are described as "Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program. "

But on the other hand,if it refers to java Errors then they are internal errors.

So is that statement is exactly true or false?

like image 311
A Diss Avatar asked Mar 16 '23 08:03

A Diss


1 Answers

The best place for such explanations is the official documentation. Throwable is the super class, under which there is Error and Exception. RuntimeException is a subclass under Exception.

  • Error & its sub-classes are unchecked.
  • Exception & its sub-classes are checked;
    • except for the RuntimeException branch.

The difference between the Error and RuntimeException classes are that

  • Error isn't in your control. They're usually some system/environment issue; e.g. OutOfMemoryError.
  • RuntimeException on the other hand represent a flaw in the logic of your program, i.e. it's in your control. You can correct it. E.g. NullPointerException
like image 139
Vineet Avatar answered Mar 27 '23 17:03

Vineet