Checked exceptions extend java.lang.Exception
, while unchecked exceptions extend java.lang.RuntimeException
, or java.lang.Error
.
Exception
extends java.lang.Throwable
, while RuntimeException
extends Exception
, and Error
, like Exception
, extends java.lang.Throwable
.
When deciding whether you should be using a checked vs. unchecked exception, always remember these rules:
Exception
S are cases an application would want to handle.
RuntimeException
S are cases you (usually) can't handle, due to programming error. You shouldn't catch RuntimeException
S, they should be targeted in your unit testing, and fixed in your production code.
Error
S are cases you can't handle because of critical errors, such as system problems (e.g. file system has failed). You shouldn't ever throw, catch or subclass an Error
, unless you're building something such as a compiler for the JVM.