Saturday, December 3, 2011

Checked vs Unchecked exceptions

Checked exceptions
represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
are subclasses of Exception

Unchecked exceptions
represent defects in the program (bugs)
Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time
are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException

http://www.javapractices.com/topic/TopicAction.do?Id=129
http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html



Exception Handling in Java, Java Exception Handling Examples - wingslive
http://www.youtube.com/watch?v=fAWvRYC90Mw&feature=related

exceptions are objects that interrupt normal flow of a program

exception=error

compile-time errors
syntax errors

run-time errors
exceptional errors




Java Exception Handling: Concepts
http://www.youtube.com/watch?v=LpfHEjEoDCg






checked exceptions are the exceptions which occur during the compile time of the program whereas Unchecked exceptions are the exceptions which occur during the run time of the program.

Checked exceptions must be explicitly caught or propagated as described in Basic try-catch-finally Exception Handling.
Unchecked exceptions do not have this requirement. They don't have to be caught or declared thrown.




Checked Exceptions:

You should compulsorily handle the checked exceptions in your code, otherwise your code will not be compiled. i.e you should put the code which may cause checked exception in try block. "checked" means they will be checked at compiletime itself.

The most perfect example of Checked Exceptions is IOException which should be handled in your code Compulsorily or else your Code will throw a Compilation Error.



Unchecked Exceptions :

Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time.

With an unchecked exception, however, compiler doesn't force client programmers either to catch the exception or declare it in a throws clause.

The most Common examples are ArrayIndexOutOfBoundException, NUllPointerException ,ClassCastException

http://www.coderanch.com/t/267483/java-programmer-SCJP/certification/Checked-Unchecked-Exceptions

No comments:

Post a Comment