Difference between checked and unchecked exception in java? - Java @ Desk

Thursday, May 23, 2013

Difference between checked and unchecked exception in java?



Checked exceptions are the ones that are checked at compile time. In order to proper compilation of the program, such exceptions need to be handled either using a try/catch block or using the throws clause.

Example : SQLException is a checked exception. If a code is trying to get the connection from database, then SQLException is the one that need to be handled for proper compilation of a program.




Unchecked exceptions are those that are not checked at compile time. Such exceptions do not affect compilation of a program and come into effect at run time. Error and RuntimException are examples of unchecked exception.

String string = null;
String.equals(“”); // This will throw NullPointerException which is an example of RuntimeException

Example of checked Exception in Java API
Following are some Examples of Checked Exception in Java library:

IOException
SQLException
DataAccessException
ClassNotFoundException

Example of unchecked Exception in Java API

Here are few examples of Unchecked Exception in Java library:

NullPointerException
ArrayIndexOutOfBound
IllegalArgumentException
IllegalStateException






No comments:

Post a Comment