5

How does the Java compiler know that java.lang.RuntimeException and its subclasses are unchecked, since they inherit from java.lang.Exception, which is a checked exception? I looked into the code and doesn't seem to be anything inside the class that tells it to the compiler.

1
  • Because it 'knows' what's written in the JLS, not just what's in the class hierarchy. Commented Feb 15, 2013 at 3:59

2 Answers 2

9

Because it is defined that way in the specification of the language # 11.1.1:

  • The unchecked exception classes are the runtime exception classes and the error classes.
  • The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.

So it is not part of the code of the various exception classes, it is only a convention defined by the language, that compilers must implement to be compliant.

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

1 Comment

To build on what @assylias wrote: all Java compilers must detect that a java.lang.RuntimeException is in use, and do whatever they need to do internally to flip from "checked" over to "unchecked". The source code doesn't get to tell the compiler "I'm an unchecked exception," it's that the compiler knows the full name of the RuntimeException class and can look for them.
1

The fact that it extends RuntimeException is enough. During compile-time, it knows what the class hierarchy is, so if it includes RuntimeException, it's unchecked. Otherwise, it's checked. Remember that checked/unchecked is a compiler constraint, not a runtime constraint, there are ways to make Java throw checked exceptions that are never caught. For example, using Proxy:

public class ExceptionalInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { throw new IOException("Take that, checked exceptions!"); } } 

And an interface:

public interface Example { public void doSomething(); } 

And create a proxy and call it:

 Example ex = (Example) Proxy.newProxyInstance(Example.class.getClassLoader(), new Class[] { Example.class }, new ExceptionalInvocationHandler()); ex.doSomething(); // Throws IOException even though it's not declared. 

8 Comments

I knew that it was engough, but how the compiler knows it?
@DanielPereira Because it has constructed a class hierarchy? If you want the exact implementation details, this isn't the place to ask. Take a look at the compiler source code from OpenJDK.
Why Stackoverflow is not a place to ask this? I thought it was a programming Q&A site.
@DanielPereira Because SO is meant to be something that's useful to everyone. Compilers and their implementations are extremely niche. All you need to know as a programmer is that the compiler checks this information using the class hierarchy.
The last time I check we still needed to use a programming language to develop a compiler, which is enough to ask it here, but nevermind.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.