2

In Java, how a compiler will identify that there is a checked exception? I mean how it is identifying it?

0

2 Answers 2

5

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.

Read

Sign up to request clarification or add additional context in comments.

Comments

1

All checked exceptions have a base class Exception, while non checked exceptions extend RuntimeException or Error.

3 Comments

There's also the potential for other subclasses of Throwable which are none of Exceptions, RuntimeExceptions, nor Errors.
All Throwable are checked, except Error and RuntimeException. If you subclass Throwable directly it is checked.
There are unchecked exceptions which satisfy both your conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.