Hi, I am trying to learn creating my exceptions in java.
My classes are :
public class Test {
public static void main(String args[]) {
Test et = new Test();
int x1=5;
int x2=0;
try {
et.printResults(x1, x2);
}
catch (DivideByZeroException dbz) {
System.out.println(dbz.toString());
dbz.printStackTrace();
}
finally {
System.out.println("The numbers are: "+x1+" "+x2);
}
}
void printResults(int a, int b) throws DivideByZeroException {
System.out.println("Add: "+(a+b));
System.out.println("Sub: "+(a-b));
System.out.println("Mul: "+(a*b));
System.out.println("Div: "+(a/b));
throw new DivideByZeroException();
}
}
public class DivideByZeroException extends ArithmeticException
{
public DivideByZeroException() {}
public DivideByZeroException(String msg) {super(msg);}
public String toString() {
return "DivideByZeroException: The denominator cannot be zero.";
}
}
But the program exits when arrives in a/b what'is wrong ??
Also could someone explain me what is the difference between printStackTrace and toString in an exception object ??
Thanks a lot