HomeComputer programmingJava programmingJava: Exceptions - Writing Your Own

Java: Exceptions – Writing Your Own

Writing your own Exceptions is really simple. The following tutorial describes the steps necessary to accomplish this.


Writing your own Exceptions is simply a case of syntax and knowing how Exceptions work. If you have not already done so, I suggest you read the two tech-recipes on Try/Catch/Finally and Throwing Exceptions.
*This tech-recipe will combine the ideas used in the Try/Catch/Finally and Throwing tutorials and will add to them a brand new object class. It is a subclass of Exception that I have written to be implemented as a new Exception.

First, we need an Exception class:
I am going to design a class to take the place of the ArithmeticException class, for the case of dividing by zero.

There are two key points to remember when writing an Exception class:
1) It must extend Exception either directly or indirectly (through one of its subclasses).
2) The constructor needs to call super() with the text to be placed into .Message and for its toString().
*All other information, such as Stack Trace, is done automatically by the JVM.

CODE example:
public class DivideByZeroException extends ArithmeticException {
public DivideByZeroException() {
super(“Attempted to divide by zero!”);
}
}

*This is all that is required. The class DivideByZeroException simply extends ArithmeticException, which if you were to read this class extends Exception. (condition 1)
Then the constructor takes no parameters, but it can. String parameters are common when an Exception will have multiple causes. In that case, the text given to the Exception can be printed for a more specific cause of the error. In either case, super must be called with a String of some kind, as is done here (condition 2).

Now that we have an Exception, we need to know how to use it. All that is needed is a simple modification to the code under the tutorial Throwing.

CODE Example:
public class ExceptionTester5 {
private static int quotient(int numerator, int denominator) throws DivideByZeroException {
if(denominator == 0)
throw new DivideByZeroException();
return(numerator / denominator);
}

public static void main(String args[]) {
int number1=0, number2=0, result=0;
try {
number1 = 1;
number2 = 0;
result = quotient(number1,number2);
System.out.print(number2 + ” goes into ” + number1);
System.out.println(” this many times: ” + result);
}
catch (Exception e) {
System.out.println(e.toString());
System.out.println(“An Exception occured”);
System.exit(-1);
}
}
}

*All that was done is the name of the Exception is now my DivideByZeroExeption.
Notice that the text printed is now the text which is in the class DivideByZeroException.

It is that simple. Now you can design custom exceptions for your programs.

Questions/Comments: [email protected]
-William. § (marvin_gohan)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!