scdmb
scdmb

Reputation: 15641

Implementation of checked and unchecked exceptions

This is not a question what they are, but how they are implemented internally. How does compiler distinguish between checked exceptions (for example derived directly from Exception except RuntimeException) and those unchecked (for example derived from RuntimeException). Are there some special flags in exception class definitions which say what exceptions must be put in try/catch blocks or is there some specific rule in Java compiler (for example Java language design explicitly specifies the type of exceptions which are checked or not)?

Upvotes: 1

Views: 244

Answers (2)

Andremoniy
Andremoniy

Reputation: 34920

All unchecked exceptions are successors from the class RuntimeException or the class Error. All others are checked. Using reflection, for example, you can always detect which class is a superclass for this specific class of the current exception. This is the same way how Java compiler detects their types.

Upvotes: 0

Isaac
Isaac

Reputation: 16736

This is enforced by the Java compiler, and also double-checked at runtime: if a Throwable extends RuntimeException or Error, then it is deemed unchecked; all others are checked.

At runtime, this check is also enforced (remember, you can theoretically "build" Java classes at runtime).

Upvotes: 2

Related Questions