2

Possible Duplicate:
Java: checked vs unchecked exception explanation
Why are exceptions named checked and unchecked?

Why is the two exception types in Java named "checked" and "unchecked"? What is the reason behind choosing this type of exceptions ?

Community
  • 1
  • 1

2 Answers2

3

Checked exceptions extend java.lang.Exception, while unchecked exceptions extend java.lang.RuntimeException, or java.lang.Error.

Exception extends java.lang.Throwable, while RuntimeException extends Exception, and Error, like Exception, extends java.lang.Throwable.

When deciding whether you should be using a checked vs. unchecked exception, always remember these rules:

  • ExceptionS are cases an application would want to handle.
  • RuntimeExceptionS are cases you (usually) can't handle, due to programming error. You shouldn't catch RuntimeExceptionS, they should be targeted in your unit testing, and fixed in your production code.
  • ErrorS are cases you can't handle because of critical errors, such as system problems (e.g. file system has failed). You shouldn't ever throw, catch or subclass an Error, unless you're building something such as a compiler for the JVM.
wulfgarpro
  • 6,666
  • 12
  • 69
  • 110
2

The class RuntimeException and its subclasses, and the class Error and its subclasses are unchecked exceptions classes.The compiler doesn’t forces them to be declared in the throws clause. All the other exception classes that are part of Throwable hierarchy are checked exceptions.

unchecked exception classes are exempted from compile-time checking in java because they can occur at many points in the program and recovery from them is difficult or impossible. They also depend on the logic of the programmer. For example the OutOfMemory Exception is thrown when JVM cannot allocate any more memory to the program

Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40