| Mobile | RSS

Implementing User Defined Exceptions 

3rd Dec, 2009 | No Comment | Posted in Java by Alex Jose

In addition to the built-in exceptions, you can create customized exception as per the application requirements. Each application that you create might have specific constraints.

For example, in case of a train reservation application, a passenger must specify the destination. Also, a ticket has to be bought for a passenger who is above three years of age.

Similarly, in the case of a banking application, if a customer is below 18 years of age, he is allowed to open a joint account only. Error handling becomes a necessity while developing applications that account for such constraints. The Exception class or its subclasses in Java do not address these application specific constraints. You can create your own exception classes to address these constraints and ensure the integrity of application data.

Creating and Handling User-defined Exceptions

The throw, throws, try, catch, and finally keywords are used while implementing userdefined exceptions. The Exception class inherits all the methods provided by the Throwable class. The user-defined Exception class also inherits the methods defined in the Throwable class. The following table lists the methods defined by the Throwable class:

Methods Description
String getMessage()

Returns a description of the
exception

String toString()

Returns a string object containing a
description of the exception. This
method is called by the println()
method when throwable object is
passed to it

Throwable fillInStackTrace()

Returns a Throwable object that
contains a stack trace

void printStackTrace() Prints the stack trace

void printStackTrace(PrintStream
stream)

Propels the stack trace to a specific
defined stream

void printStackTrace(PrintWriter
stream)

Propels the stack trace to a specific
defined stream

String getLocalizedMessage

Provides a localized description of
the exception.

 

You can use the following code to create a user-defined exception to catch the exception raised when the sum of two numbers exceeds 20:

class UserException extends Exception
{
int num1,num2,sum;
UserException(int a,int b)
{
num1 = a; num2 = b; sum = a + b;}
public String toString()
{
return "UserException Caught: The sum of the numbers Exceeds
20.."; }
}
class UserExceptionDemo
{
static void calculate(int a,int b) throws UserException
{
int sum;
System.out.println("Calculate Method(" + a + "," + b +
")");
sum=a+b;
if(sum>20)
throw new UserException(a,b);
System.out.println("The Value of the sum of two numbers
is:" + sum);
}
public static void main(String args[])
{
try
{
calculate(12,1);
calculate(15,7);
}
catch (UserException Obja)
1B.42 Programming in Java
{
System.out.println("Caught:" + Obja);
}
}
}

In the preceding code, the UserException subclass of the Exception class is created. The UserException class has one constructor, UserException() and the toString() method that returns a string containing the description of the exception. The UserExceptionDemo class defines the calculate() method that throws an object of the user-defined exception, UserException. The exception is thrown when the sum of two numbers passed as parameters to the calculate() method exceeds 20.

The output of the preceding code is:

Creating User-defined Exceptions

Related Posts with Thumbnails