Technically, the distinction is not really made between the "fatal error" and the "error being restored", but between checked exceptions and unchecked exceptions. Java distinguishes them as follows:
- you must declare an exception in your
throws ; if you use a method that throws a checked exception in a try block, you must either catch specified exception or add this exception to your throws method; - you can throw an unchecked exception in your
throws (not recommended); if you use a method that throws an exception in the try block, you can catch exclude or add this exception to your throws method (not recommended).
What, of course, is not recommended if you really donโt know what you are doing, is to " catch " any exception (for example, catch with an empty block).
Exception is a class of checked exceptions; Error and RuntimeException are both thrown exceptions, and they are all subclasses of them. You will notice that all three classes expand on Throwable , and the javadoc for Throwable states that:
For purposes of checking compile time exceptions, Throwable and any subclass of Throwable that is also not a subclass of RuntimeException or Error are considered checked exceptions.
Classic examples (c) of famous unchecked exceptions:
OutOfMemoryError (extends Error );StackOverflowError (extends Error );NullPointerException (extends RuntimeException );IllegalArgumentException (extends RuntimeException );- etc.
The only real difference between Error and RuntimeException is their estimated severity level and is a โsemantic" difference, not a technical difference: in the end, both behave the same. Some IDEs (Intellij IDEA come to mind) will also shout at you if you catch Error but don't flip it.
source share