4

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?

2
  • 4
    21 days seriously?? :-) Commented Jul 19, 2015 at 14:33
  • 2
    An Error is not an Exception at all. An Error extends directly from Throwable and is unchecked. And references to errors shouldn't be confused with the java type Error. Commented Jul 19, 2015 at 14:36

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks very much for the answer:) I do know the hierarchy of throwable,what I'm confused with is (Unchecked exceptions== runtime exceptions) part? Errors are also Unchecked exceptions as I've taught!
Yes, you're right. Both Error & RuntimeException are unchecked exceptions.
So is "Unchecked exceptions, also called runtime exceptions" wrong? because runtime exceptions are just a subset of unchecked exceptions and not equivalent terms.
Well, it depends on the context. We know that there is nothing we can do if we encounter an Error. So, there isn't much talk about it. So, the only unchecked exceptions we're interested are the RuntimeException. That's why, many simply generalize - unchecked exceptions aka run-time exceptions. But if you want to get technical, then yes, it would be wrong. Unchecked exceptions can be either of Error or RuntimeException.
Ah,it clarifies the confusion I had :)
1

Error should not be caught because it's caused by a serious problem. RuntimeException are thrown only at runtime and the compiler does not complain when you do not put fence the code that could throw them. Checked exception should be caught or send the exception at the calling level

Comments

0

A Runtime exception doesn't need to have a try-catch block, nor does the constructor or method need a throws statement.

The docs page from oracle does some good explaining.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.