Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definition of checked Java exception?

I see that one definition can be this:

Generally RuntimeExceptions are exceptions that can be prevented programmatically.

But that is still not the definition of a checked exception. I thought checked exceptions were "exceptions that can be handled at compile-time". Is that correct and/or can you tell me more?

I also read this on the site, can you explain the quote?

Many people say that checked exceptions (i.e. these that you should explicitly catch or rethrow) should not be used at all.

Java: checked vs unchecked exception explanation

Can I just learn what the definition is? I also read somewhat unexpectedly:

NumberFormatException is unchecked`

But I would think that NumberFormatException is checked since I would handle that at compile-time. Can you please help me understand? I've done some Java programming but I never wrote my own exception class, why would I need that? enter image description here

Update

A definition is given is the SCJP book by Sierra / Bates:

enter image description here

like image 300
Niklas Rosencrantz Avatar asked Apr 21 '13 14:04

Niklas Rosencrantz


3 Answers

A checked exception is defined as any subclass of java.lang.Throwable (including Throwable itself) which is not a subclass of java.lang.Error or java.lang.RuntimeException. The guidelines you saw are just that, guidelines, designed to help you understand the intent of runtime exceptions.

See the Java Language Specification, section 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.

like image 127
Antimony Avatar answered Oct 05 '22 14:10

Antimony


A more broad definition is:

CheckedExceptions are exceptions that you have to deal with explicitly. You either have to declare you can throw it or catch it and deal with it. Cunningham Wiki

As a result RuntimeExceptions are not checked, but NumberFormatException is checked if a method declares that it throws it and you are forced to catch or re-throw.

like image 28
melbyts Avatar answered Oct 05 '22 14:10

melbyts


Checked exceptions allow you to verify at compile time that you are either handling exceptions or declaring them to be thrown.

Some do suggest avoiding checked exceptions. However, this does not eliminate the cost of managing exceptions. It shifts and magnifies it, from compile-time to run time and debug time.

From the Java tutorial:

[P]rogrammers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException. Both of these shortcuts allow programmers to write code without bothering with compiler errors and without bothering to specify or to catch any exceptions. Although this may seem convenient to the programmer, it sidesteps the intent of the catch or specify requirement and can cause problems for others using your classes.

Unchecked exceptions allow you to not have to declare everywhere exceptions that can occur almost anywhere. From the Java tutorial:

Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can).

NumberFormatException is unchecked because it falls within this camp. In many applications, number format exceptions can occur almost anywhere, and be numerous.

like image 21
Andy Thomas Avatar answered Oct 05 '22 12:10

Andy Thomas