Printing a Stack Trace
When an exception is caught, you can find out the method or the line of the code where the exception is raised by using the printStackTrace() method. You can call the printStackTrace() method for printing the stack trace. The objects of the Throwable class inherit the stack trace. You can use the following code to print the stack trace for the ArithmeticException exception:
class PrintStack
{
public static void main(String args[])
{
int Num1= 30 , Num2 = 0;
try
{
int Num3=Num1/Num2;
}
catch(ArithmeticException obj)
{
obj.printStackTrace();
}
}
}
In the preceding code, printStackTrace() method is used to print the stack trace to check the line in the code where the exception is raised.
The output of the preceding code is:
Rethrowing an Exception
A catch block can rethrow an exception without handling it by using the throw statement. Exceptions are rethrown if a method causing the error wants to pass the error handling responsibilities to the exception-handlers in some other methods. You can use the following code snippet to rethrow an exception:
catch(Exception e)
{
System.out.println(“Exception Raised”);
throw e;
}
The exception goes to the catch block of the next higher context ignoring the catch blocks of the same try block.
You can use the following code to catch the NullPointerException and rethrow the exception to the outer handler:
class RethrowException
{
static void throwDemo()
{
try
{
throw new NullPointerException (“My Exception”);
}
catch(NullPointerException e)
{
System.out.println(“Exception caught in throwDemo()
method”);
throw e; // Rethrow the Exception.
}
}
public static void main(String args[])
{
try
{
throwDemo();
}
catch(NullPointerException e)
{
System.out.println(“Exception caught:” + e);
}
}
}
In the preceding code, the catch block in the throwDemo() method rethrows the NullPointer exception to the catch block defined in the main() method. The output of the preceding code is:
Using the throws Statement
The throws statement is used by a method to specify the types of exceptions the method throws. If a method is capable of raising an exception that it does not handle, the method must specify that the exception have to be handled by the calling method. This is done using the throws statement. The throws clause lists the types of exceptions that a method might throw. The following syntax shows how to declare a method that specifies a throws clause:
[<access_specifier>] [<modifier>] <return_type> <method_name>
[<arg_list>] [throws <exception_list>]
You can use the following code to throw the ClassNotFoundException exception:
class NoClassexception
{
static void throwMethod()
{
System.out.println(“In throwMethod”);
throw new ClassNotFoundException();
}
public static void main(String args[])
{
throwMethod();
// no try and catch statements to handle the exception
}
}
In the preceding code, the code does not compile because the caller method throws an exception but does not catch the exception. You can compile the code by first declaring that the throwMethod() throws the ClassNotFoundException exception. In addition, the main() method must define the try-catch statements to catch the exception. You can use the following code to use the throws statement:
class ThrowsClass
{
static void throwMethod() throws ClassNotFoundException
{
System.out.println(“In throwMethod”);
throw new ClassNotFoundException();
}
public static void main(String args[])
{
try
{
throwMethod();
}
catch(ClassNotFoundException Obja)
{
System.out.println(“throwMethod has thrown an
Exception:” + Obja);
}
}
}
The output of the preceding code is:




