Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify checked and unchecked exceptions in java?

While reading about exception, I will always come across checked exceptions and unchecked exceptions, So wanted to know how to distinguish that which is what?

Edit: I want to know if i create any exception class then how can i create as a checked or as an unchecked?

and what is the significance of each?

like image 308
GuruKulki Avatar asked Mar 10 '10 17:03

GuruKulki


People also ask

What is the main difference between a checked and unchecked exception?

1. Checked exceptions happen at compile time when the source code is transformed into an executable code. Unchecked exceptions happen at runtime when the executable program starts running.

Which exceptions are unchecked in Java?

Some common unchecked exceptions in Java are NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.

Is Indexoutofbounds checked or unchecked?

The StringIndexOutOfBoundsException is an unchecked exception in Java that occurs when an attempt is made to access the character of a string at an index which is either negative or greater than the length of the string.


2 Answers

See the Java Language Spec, chapter 11:

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.

You could check this via instanceof at runtime, though I don't really see where this would be useful.

As to the second part of your question:

  • checked exception represent expected error conditions, which can occur during normal program execution and therefore always have to be handled programatically (which the compiler enforces)

  • unchecked exception represent unexpected error conditions and signify an abnormal state of your program, due to invalid input, bugs or runtime restrictions (eg memory); the compiler won't force the programmer to handle these, ie you only have to care for them if you know of their occurrence

like image 30
Christoph Avatar answered Oct 24 '22 00:10

Christoph


All Throwables except subclasses of java.lang.RuntimeException or java.lang.Error are checked. Properly, in Java, "exceptions" are subclasses of java.lang.Exception, "errors" are subclasses of java.lang.Error and java.lang.Throwable is not usually subclassed directly.

Programs are not supposed to create their own Error subclasses (though the documentation is rather ambiguous on that) so generally you always create Exceptions, using a RuntimeException if you don't want it to be checked.

To know at run-time if you have a checked exception you could use:

if(throwable instanceof Exception && !(throwable instanceof RuntimeException)) {
    // this is a checked Exception
    }

A checked exception is one which must be either handled in a catch clause, or declared as being thrown in the method signature; the compiler enforces this. Generally, one uses checked exceptions for exceptions which should be handled by the calling code, while unchecked exceptions are for conditions which are the result of a programming error and should be fixed by correcting the code.

That said there is much debate in the Java community about the efficacy of using checked exceptions vs. unchecked exceptions everywhere - a subject way to deep to discuss in this answer.

EDIT 2012-10-23: In response to comments (which are quite valid), to clarify, the following would be what is required to determine if a captured Throwable is a checked Throwable as opposed to a checked Exception:

if(obj instanceof Throwable && !(obj instanceof RuntimeException) && !(obj instanceof Error)) {
    // this is a checked Throwable - i.e. Throwable, but not RuntimeException or Error
    }

If the object in question is known to be an instance of Throwable (e.g. it was caught), only the second part of the above 'if' is needed (e.g. testing for Throwable is redundant).

like image 61
Lawrence Dol Avatar answered Oct 23 '22 23:10

Lawrence Dol