问题
I read about Checked exception that it is checked by compiler , but its runtime only.
Is it correct?
If it is correct then how?
回答1:
Checked exceptions are checked at compile time to ensure you are handling them, either by catching them or declaring the containing method throws
the exception.
At runtime, there is no distinction between checked and unchecked exceptions: they are treated identically by the JVM. So "checked-ness" is purely a compile-time concept.
回答2:
Runtime exceptions are not checked exceptions. Checked exceptions are the class Exception
and all subclasses except for RuntimeException
.
Checked exceptions need to be either handled in your code or declared in the method's signature otherwise your code won't compile. Runtime exceptions do not need to be handled or declared.
来源:https://stackoverflow.com/questions/50549579/checked-exception-is-compile-time-or-runtime