RuntimeException is thrown at runtime, right?
Note: This is not about the controversy of unchecked exceptions!
For that you should read this:
Well, yes. It is thrown at runtime. Just like all other exceptions. There are also compiler errors, but exception handling is always done at runtime.
So what is a
From the javadoc:
RuntimeExceptionis the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
OK, that only helps when you understand what normal means in this context. I’m not entirely sure I understand what they mean by it. If the JVM doesn’t run in normal operation then who cares about exceptions? And what’s the name of those exceptions that can be thrown during the abnormal operation of the JVM?
But the interesting part follows:
RuntimeExceptionand its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor’s
throwsclause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
Well then why don’t they call it
UncheckedException? Any how would it be normal to just throw such an unchecked exception? Aren’t those used to indicate abnormal behaviour?
Oracle says this in the tutorial:
Runtime exceptions represent problems that are the result of a programming problem
If a client cannot do anything to recover from the exception, make it an unchecked exception.
So why not call it
UnrecoverableException? The name
RuntimeException does in no way express the actual meaning of the exception.
Another problem is that we have to extend some interface just to make an exception unchecked. But they didn’t have annotations back then, so this was justified.
RuntimeException exists since version 1.0 of Java.