Java Tutorial : Built-in Exceptions

The built-in exceptions in Java are categorized on the basis of whether the exception is handled by the Java compiler or not. Java consists of the following categories of built-in exceptions:

  • Checked Exceptions
  • Unchecked Exceptions

Checked Exceptions

Checked exceptions are the objects of the Exception class or any of its subclasses excluding the Runtime Exception class. Checked exceptions are the invalid conditions that occur in a Java program due to invalid user input, network connectivity problem, or database problems. For example, java.io.IOException is a checked exception. The IOException exception is thrown whenever an input/output operation is abnormally terminated.

Java uses the try-catch block to handle the checked exceptions. The statements within a program that throw an exception are placed in the try block. You associate an exception-handler with the try block by providing one or more catch handlers immediately after the try block.

The following table lists the various checked exceptions defined in the java.lang package:

Exception

Cause of Creation

ClassNotFoundException

Occurs when the Java run-time
system is unable to find the class
referred.

IllegalAccessException

Occurs when you want to refer a
class that is not accessible.

InstantiationException

Occurs when you try to create an
object of an abstract class or
interface.

NoSuchMethodException

Occurs when you call a method that
does not exist.

 

Unchecked Exceptions

Unchecked exceptions are the run-time errors that occur because of programming errors, such as invalid arguments passed to a public method. The Java compiler does not check the unchecked exceptions during program compilation. For example, if you divide a number by zero, an unchecked or run-time exception is raised.

The following table lists the various unchecked exceptions:

Exception

Cause of Creation

ArithmeticException

Occurs when you make an
arithmetic error, such as dividing a
number by zero.

ArrayIndexOutOfBoundsException

Occurs when an attempt is made
to access an array element beyond
the index of the array.

ArrayStoreException

Occurs when you assign an
element to an array that is not
compatible with the data type of
that array.

ClassCastException

Occurs when you assign a
reference variable of a class to an
incompatible reference variable of
another class.

IllegalArgumentException

Occurs when you pass an
argument of incompatible data
type to a method.

NegativeArraySizeException

Occurs when you create an array
with negative size.

NullPointerException

Occurs when an application tries to
use an object without allocating
memory to it or calls a method of
a null object.

NumberFormatException

Occurs when you want to convert
a string in an incorrect format to a
numeric format.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>