Difference between Unchecked exception or runtime exception
This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the pr开发者_开发知识库ogram.
As stated by their name, unchecked exceptions are not checked at compile-time which means that the compiler doesn't require methods to catch or to specify (with a throws
) them. Classes belonging to this category are detailed in the section 11.2 Compile-Time Checking of Exceptions of the JLS:
The unchecked exceptions classes are the class
RuntimeException
and its subclasses, and the classError
and its subclasses. All other exception classes are checked exception classes. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers. See §11.5 for a description of the exception class hierarchy and some of the exception classes defined by the Java API and Java virtual machine.
The following picture illustrates the Exception hierarchy:
The class Error
and its subclasses are exceptions from which ordinary programs are not ordinarily expected to recover and, as explained in 11.5 The Exception Hierarchy:
The class
Error
is a separate subclass ofThrowable
, distinct fromException
in the class hierarchy, to allow programs to use the idiom:} catch (Exception e) {
to catch all exceptions from which recovery may be possible without catching errors from which recovery is typically not possible.
To summarize, RuntimeException
are a subset of unchecked exceptions for exceptions from which recovery is possible (but unchecked exception is not a synonym of RuntimeException
as many are answering here).
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
.
Note: a RuntimeException IS an unchecked exception
An unchecked exception would be one that is known to be possible at a point in the execution but is not caught, for example a NullPointerException is always a possibility if you don't check for them and will cause your program to terminate. You could check for it by wrapping code in try-catch, but this is not enforced (unlike a checked exception that will enforce that the exception is handled in some way).
An error is something that can occur at any point during execution and can't really be caught because it is not eplicitly caused by a particular method call etc. For example an OutOfMemoryError or a StackOverflowError. Both of these could occur at any time and will cause your application to terminate. Catching these errors make no sense as they indicate that something has happened that you won't be able to recover from.
Errors indicate fundamental problems that should never occur. If you run into an error s.th. really bad happened.
Unchecked Exceptions (Runtime Exceptions) on the other hand are used whenever an exception could be expected somehow but there is no reasonable way to deal with it then and thus a try catch statement would be just annoying and a waste of space.
Checked Exception:
- The classes that extend
Throwable
class exceptRuntimeException
andError
are known as checked exceptions. - Also known as compile time exception because these type of exceptions are checked at compile time. That means if we ignore these exception (not handled with
try/catch
orthrow
the exception) then a compilation error occurred. - They are programmatically recoverable problems which are caused by unexpected conditions outside control of code (e.g. database down, file I/O error, wrong input, etc)
- We can avoid them using
try/catch
block. - Example:
IOException
,SQLException
etc
Unchecked Exception:
- The classes that extend
RuntimeException
are known as unchecked exceptions - Unchecked exceptions are not checked at compile-time rather they are checked at runtime.And thats why they are also called "Runtime Exception"
- They are also programmatically recoverable problems but unlike checked exception they are caused by faults in code flow or configuration.
- Example:
ArithmeticException
,NullPointerException
,ArrayIndexOutOfBoundsException
etc - Since they are programming error, they can be avoided by nicely/wisely coding. For example "dividing by zero" occurs
ArithmeticEceeption
. We can avoid them by a simple if condition -if(divisor!=0)
. Similarly we can avoidNullPointerException
by simply checking the references -if(object!=null)
or using even better techniques
Error:
Error
refers irrecoverable situation that are not being handled by try/catch- Example:
OutOfMemoryError
,VirtualMachineError
,AssertionError
etc.
Error: These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.
Runtime exception : These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.
You may want to read this :
RuntimeExceptions
and Errors like OutOfMemoryError
don't need to be catched and can be thrown until they reach main() which will terminate the application.
Other Exceptions cause an compile error if they are not catched or included in the throws list.
Errors and runtime exceptions are collectively known as unchecked exceptions.
runtime exceptions are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API
You may want to take a look at this link which explains the Three Kinds of Exceptions.
http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html
I hope this helps.
Both java.lang.Error and java.lang.Exception are the sub classes of java.lang.Throwable.
java.lang.Error class represents the errors which are mainly caused by the environment in which application is running. For example, OutOfMemoryError occurs when JVM runs out of memory or StackOverflowError occurs when stack overflows.
Where as java.lang.Exception class represents the exceptions which are mainly caused by the application itself. For example, NullPointerException occurs when an application tries to access null object or ClassCastException occurs when an application tries to cast incompatible class types.
All the sub classes of java.lang.Exception (except sub classes of RunTimeException) are checked exceptions. For example, FileNotFoundException, IOException, SQLException, ClassNotFoundException etc…
All the sub classes of java.lang.RuntimeException and java.lang.Error are unchecked exceptions. For example, NullPointerException, ArithmeticException, ClassCastException, ArrayIndexOutOfBoundsException, StackOverflowError, OutOfMemoryError etc…
Source : Error Vs Exceptions, Checked Vs Unchecked Exceptions
精彩评论