Tag Archives: Using Throw Statement

Java Tutorial : Throwing An Exception – Part 2

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.

Using the printStackTrace Statement

Using the printStackTrace Statement

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:

Rethrowing an Exception

Rethrowing an Exception

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:

Using the Throws Statement

Using the Throws Statement

Java Tutorial : Throwing An Exception – Part 1

You can throw an exception explicitly using the throw statement. For example, you need to throw an exception when a user enters a wrong loginID or password. The throws clause is used to list the types of exceptions that can be thrown during the execution of a method in a program.

Using the throw Statement

The throw statement causes termination of the normal flow of control of the Java code and stops the execution of the subsequent statements if an exception is thrown when the throw statement is executed. The throw clause transfers the control to the nearest catch block handling the type of exception object throws. If no such catch block exists, the program terminates. The throw statement accepts a single argument, which is an object of the Exception class.

The following syntax shows how to declare the throw statement:

throw ThrowableObj

In the preceding syntax, ThrowableObj is an object of the Throwable class or a subclass of the Throwable class, such as Exception class. The ThrowableObj object is created using the new operator. The Java compiler gives an error if the ThrowableObj object does not belong to a valid Exception class. You can use the following code to throw the IllegalStateException exception:

class ThrowStatement
{
static void throwDemo()
{
try
{
throw new IllegalStateException();
// creating and throwing an object of the
IllegalStateException class.
}
catch(NullPointerException objA)
{
System.out.println(“Not caught by catch block inside
throwDemo().”);
}
}
public static void main(String args[])
{
try
{
throwDemo();
}
catch(IllegalStateException objB)
{
// catching an object of the
IllegalStateException class.
System.out.println(” Exception Caught in :” + objB);
}
}
}

In the preceding code, the new operator is used to construct an object of IllegalStateException. In the throwDemo() method , an exception of the type IllegalStateException is thrown.

The output of the preceding code is:

Using the Throw Statement

Using the Throw Statement

Displaying the Description of an Exception

The built-in exception classes in Java have two types of constructors: one with no parameter and another with a String parameter that defines the exception. The String parameter that you pass to the constructor of an exception can be displayed when exception object is passed as an argument to the System.out.println() statement. The following code shows the use of constructor of the IlegalStateException with String parameter:

class ThrowClass
{
static void throwDemo()
{
try
{
throw new IllegalStateException(“MyException”);
}
catch(IllegalStateException objA)
{
System.out.println(“Caught:” + objA);
}
} // End of throwDemo() method.
public static void main(String args[])
{
throwDemo();
}
}// End of the ThrowClass.

In the preceding code, description added to the constructor of the IllegalStateException exception is displayed as MyException.

The output of the preceding code is:

Displaying the Description of an Exception

Displaying the Description of an Exception