Java Throwing Exceptions - Exercise Result
You Said:
classSqRoot2 {
public static void main(String[] args) {
try {
double d = getInput(args[0]);
double root = Math.sqrt(d);
System.out.println("The square root of " + d + " is " + root);
}
catch(NumberFormatException e) {
System.out.println("Be sure to enter a number.");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Enter number as 1st parameter.");
}
catch(ImNumberException e) {
System.out.println("Result will be imaginary number.");
}
}
static double getInput(String s) throws ImNumberException {
double d = new Double(s).doubleValue();
if (d < 0)
throw new ImNumberException();
return d;
}
}
classImNumberException extendsException {}