Java built-in exception concept


Java defines several exception classes inside the standard package java.lang.The most general of these exceptions are subclasses of the standard type RuntimeException. Since java.lang is implicitly imported into all Java programs, most exceptions derived from RuntimeException are automatically available.
Java defines several other types of exceptions that relate to its various class libraries. The built-in exceptions in Java are categorized on the basis of whether the exception is handled by the Java compiler or not.

Inside the standard package java.lang ,java defines several exception classes .A few have been used by the proceeding examples.The  most general of these exceptions are subclasses of the standard type RuntimeException .Since java.lang  is implicitly imported into all java programs,most exceptions derived from RuntimeException are automatically available .Furthermore ,they need not be included in any method's throws list.

 Java consists of the following categories of built-in exceptions:

  • Checked Exceptions
  • Unchecked Exceptions 
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 Description
ArithmeticException Arithmetic error, such as divide-by-zero.
ArrayIndexOutOfBoundsException Array index is out-of-bounds.
ArrayStoreException Assignment to an array element of an incompatible type.
ClassCastException Invalid cast.
IllegalArgumentException Illegal argument used to invoke a method.
IllegalMonitorStateException Illegal monitor operation, such as waiting on an unlocked thread.
IllegalStateException Environment or application is in incorrect state.
IllegalThreadStateException Requested operation not compatible with current thread state.
IndexOutOfBoundsException Some type of index is out-of-bounds.
NegativeArraySizeException Array created with a negative size.
NullPointerException Invalid use of a null reference.
NumberFormatException Invalid conversion of a string to a numeric format.
SecurityException Attempt to violate security.
StringIndexOutOfBounds Attempt to index outside the bounds of a string.
UnsupportedOperationException An unsupported operation was encountered.

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 Description
ClassNotFoundException Class not found.
CloneNotSupportedException Attempt to clone an object that does not implement the Cloneable interface.
IllegalAccessException Access to a class is denied.
InstantiationException Attempt to create an object of an abstract class or interface.
InterruptedException One thread has been interrupted by another thread.
NoSuchFieldException A requested field does not exist.
NoSuchMethodException A requested method does not exist.

0 comments:

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org