Reputation: 1533
In Java, how a compiler will identify that there is a checked exception? I mean how it is identifying it?
Upvotes: 2
Views: 3843
Reputation: 20323
Right from the docs:
A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (§8.4.6) or constructor (§8.8.5) must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.
The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers. See §11.5 for a description of the exception class hierarchy and some of the exception classes defined by the Java API and Java virtual machine.
So basically it looks at the code, if it comes across an exception, looks up the inheritance hierarchy of the exception to decide if it is checked or unchecked.
Upvotes: 5
Reputation: 4444
All checked exceptions have a base class Exception
, while non checked exceptions extend RuntimeException
or Error
.
Upvotes: 1