Java checked vs unchecked Exception - tell me only from code?

Is it possible to determine if the exception class is checked or not checked just by looking at the code? I always thought that if he extended Exception, he was checked, but then RuntimeException extends Exception and is not monitored. A RuntimeException may be the only class that rejects this rule of thumb, and other unchecked exceptions should extend the Throwable if not to extend a RuntimeException. However, I do not see how RuntimeException differs from Exception. I wonder if the difference is defined inside the interpreter itself?

+6
source share
8 answers

Is it possible to determine if the exception class is checked or not checked just by looking at the code?

Yes. If you know the rules ... as stated in JLS ( 11.1.1 ) ... and you can also see the superclass exception code (so you can check the hierarchy).

The rules are that exceptions are "checked", with the following exceptions:

  • RuntimeException and its subclasses and

  • Error and its subclasses

which are not marked.

I wonder if the difference is defined inside the interpreter itself?

No. This is in the Java Language Spec. In fact, the JVM treats checked and unchecked exceptions the same. All verification that checked exceptions are handled correctly is done by the Java compiler.


However, I still don't understand the reasoning that RuntimeException extends Exception, not Throwable. This design choice seems contradictory given that there is nothing in RuntimeException that overrides the behavior defined in Exception.

The way it is. And besides, I do not see any logical contradictions.

  • An Error is an unrecoverable condition. It is not installed because it makes no sense to force the application to do something with it.

  • An Exception represents a potentially recoverable condition.

  • A RuntimeException is a potential condition that we do not want to force the application to process. (But he could if he wanted to).

Obviously, according to this taxonomy, a RuntimeException → is <a Exception and → not <a Error ... and this is the rationale for defining the hierarchy of exception classes in this way.

+2
source

RuntimeException , and its subclasses are thrown exceptions. All others are checked by exceptions.

+4
source

You can find the definition of unchecked exceptions in the Java Tutorials (my highlight):

... Java programming language does not require methods to catch or to indicate thrown exceptions (RuntimeException, Error and their subclasses) ...

+2
source

Instead of “caught versus unclaimed” they are called “verified or unverified” exceptions. During the check, exceptions are checked, i.e. The compiler warns you if something does not match the exception contract, but uncontrolled ones can be reset at runtime.

+1
source

If you are using an IDE, this is the easiest way if if your IDE gives you an error / underline message that you have an unhandled exception if you do not catch it. These are checked exceptions.

Alternatively, anything that inherits from a RuntimeException is unchecked.

0
source

The inheritance tree for Throwable actually goes:

  Throwable error
               Exception RuntimeException

Places that throw anything that extends Error or RuntimeException should not be declared. Errors usually cause only the JVM and signal that it is happening very badly.

Any Exception extension, but not a RuntimeException extension, must be declared. Typically, Exceptions are things that should be handled or treated by the calling code (for example, “cannot open the file”), while RuntimeExceptions are things that probably indicate a code error or corrupted data, such as NullPointerException and therefore unlikely so that the calling code can do something about it.

0
source

If you use any method or code that throws exception explicitly, you need to catch this exception and, after looking at the code, we are sure that we need to catch so that this is thrown by Exception . In the case of a RuntimeException , by looking at the code, you cannot guarantee that this code throws an exception, so they are not marked.

Java Document Reference

Any exception that can be thrown by a method is part of the public programming interface of the method. Those who call the method should be aware of the exceptions that the method may throw so that they can decide what to do with them. These exceptions are as much a part of the method programming interface as its parameters and return value.

0
source

As javadoc said:

RuntimeException is a superclass of those exceptions that may be thrown during the normal operation of the Java virtual machine. the method does not need to declare in its throws article any subclasses of RuntimeException that can be thrown at runtime but not caught.

Author (s): Frank Yellin

Since: JDK1.0

Now, back to your question, I wonder if the difference is defined inside the interpreter itself? .... the magic trick is performed inside the bytecode .

The following article demonstrates this by analyzing bytecode:

http://www.javaworld.com/article/2076868/learn-java/how-the-java-virtual-machine-handles-exceptions.html

The main mecanism is the exception table:

 Exception table: from to target type 0 4 4 <Class java.lang.ArithmeticException> 

If an exception is thrown during the execution of the method, the Java virtual machine searches the exception table for the corresponding record.

An exception table entry matches if the current program counter is within the range indicated by this entry, and if the class of the excluded class is the exception class specified in the record (or is a subclass of the specified exception class).

The Java virtual machine searches the exception table in the order in which the entries appear in the table. When the first match is found, the Java virtual machine sets the program counter to the new location of the PC offset and continues execution there.

If no match is found, the Java virtual machine pops the current stack stack and raises the same exception.

When the Java virtual machine pops the current stack stack, it actually interrupts the execution of the current method and returns the method that called this method. But instead of the usual execution of the previous method, it throws the same exception in this method, which causes the Java virtual machine to go through the same search process on the exception table of this method.

0
source

All Articles