Built-in Exceptions in
Java:
In the Java’s
standard package java.lang several
exception classes are defined. In Java exceptions are classified as unchecked
and checked exceptions.
Unchecked exceptions are the exceptions
that are not checked by the complier, to see whether the methods handle it or
not. It is not necessary for a method to include these exceptions in throws
list.
Checked exceptions are exceptions that
are checked by the complier to see whether the method handles or throws these
exceptions. These exceptions have to be specified in method’s throws
list.
The unchecked
and checked exceptions defined in java.lang
package are listed in table below.
Unchecked Exceptions
|
Checked Exceptions
|
ArithmeticException
|
ClassNotFoundException
|
NumberFormatException
|
CloneNotSupportedException
|
ArrayIndexOutOfBoundsException
|
IllegalAccessException
|
NegativeArraySizeException
|
InstantiationException
|
ArrayStoreException
|
InterruptedException
|
IllegalArgumentException
|
NoSuchFieldException
|
IllegalMaonitorStateException
|
NoSuchMethodException
|
IllegalThreadStateException
|
|
IllegalStateException
|
|
IndexOutOfBoundsException
|
|
ClassCastException
|
|
EnumConstantNotPresentException
|
|
NullPointerException
|
|
NumberFormatException
|
|
SecurityException
|
|
StringIndexOutOfBounds
|
|
TypeNotPresentException
|
|
UnsupportedOperationException
|
Creating Your Own
Subclass:
It is possible to create our own exception class. You just
have to define a subclass of Exception
class. The Exception class is subclass of Throwable
class. The Throwable class defines methods that are available in all its
subclasses.
The following
two constructors defined by Exception can be used to create your own exception.
Exception()
Exception(String describeException)
In the above general form of constructors you can see one form of constructor has no
parameters. The other takes String argument. So that exception can be
described.
Consider
the below example program that creates a new exception subclass of Exception
class.
package
blog.exception.handler;
public
class ExceptionSubclass extends
Exception
{
String exDetail;
ExceptionSubclass(String s)
{
exDetail=s;
}
@Override
public
String toString()
{
return
"blog.exception.handler: "+exDetail;
}
}
package
blog.exception.handler;
public
class ExceptionSubclassDemo
{
static
void XMethod(String s)throws
ExceptionSubclass
{
throw
new ExceptionSubclass(s);
}
public
static void
main(String[] args)
{
try
{
XMethod("am
exception created by you");
}
catch(ExceptionSubclass
ex)
{
System.out.println(ex);
}
}
}
Output:
blog.exception.handler:
am exception create by you
In the above example program you can see the exception ‘ExceptionSubclass’ is created. The
method XMethod() present in the
class ExceptionSubclassDemo throws
this exception. See the output of the program, the description of the exception
is returned by the toString() method
present in the exception class ExceptionSubclass.
Note: The
toString() method is defined by String class. This method is overridden by the
Throwable class. So it is available to all the subclasses of the Throwable
class.
The methods defined by Throwable class are listed below in the
table.
Methods Defined in Throwable Class
|
Throwable fillinStackTrace()
|
Throwable getCause()
|
Throwable initCause(Throwable causeOfException)
|
String getMessage
|
String toString()
|
String getLocalizedMessage()
|
StackTraceElement[] getStackTrace()
|
void printStackTrace()
|
void printStackTrace(PrintStream stream)
|
void setStackTrace(StackTraceElement ele[])
|
Chained Exception:
The chained exception was introduced to Java from the beginning
of JDK 1.4. This feature permits you to link an exception with another exception.
Here the second exception describes the cause of first exception.
To
implement chained exception Java provides you with two constructors and two
methods that are defined in Throwable
class. The general from of these constructors and methods are shown below.
The constructors defined in Throwable.
Throwable
(Throwable causeOfException)
Throwable (String msg,
Throwable causeOfException)
The methods defined in Throwable.
Throwable getCause()
Throwable initCause(Throwable causeOfException)
Consider the below example program that creates the
chained exception.
package
blog.exception.handler;
import
java.io.IOException;
public
class ChainedXeceptionDemo
{
static
void Xmethods()
{
ArithmeticException
ax=new ArithmeticException("divide
by zero,am caused by IOException");
ax.initCause(new
IOException("am the cause"));
throw
ax;
}
public
static void
main(String[] args)
{
try
{
Xmethods();
}
catch(ArithmeticException ex)
{
System.out.println(ex);//ArithmeticException
caught.
System.out.println(ex.getCause());//IOException
caught.
}
}
}
Output:
java.lang.ArithmeticException:
divide by zero,am caused by IOException
java.io.IOException:
am the cause
In the above
example ArithmeticException is linked with its cause exception, IOException.
The method initCause(Throwable exCause) creates a new exception and links it with
the existing exception. Here, it creates IOException and links it with the
AirthmeticException. The getCause() method returns the exception that underlies
the current exception. Here current exception is AirthmeticException. So
IOException is the underlying exception that is returned by getCause() method.