There is a NumberFormatException. This exception class is automatically thrown. Refer the code below
Code:
String strNumber = "addfeff";
try {
double aDouble = Double.parseDouble(strNumber);
}
catch (NumberFormatException ex) {
// the user entered something that was not parseable into a number (e.g. letters)
}
There is an exception for divide by zero, plz check this code below for operations by zero
Code:
double numerator = 5;
double denominator = 0;
double quotient = numerator / denominator;
System.out.println("result: " + quotient);
I am posting another code, this is a general example for exceptions classes
Code:
double numerator = -6;
double denominator = 0;
if (numerator < 0) {
throw new IllegalArgumentException("numerator cannot be less than zero: " + numerator);
}
if (denominator <= 0) {
throw new IllegalArgumentException("denominator cannot be negative or zero: " + denominator);
}
// in this example, because of the checking above, we would not get here, exception would be thrown instead.
double quotient = numerator / denominator;
System.out.println("result: " + quotient);
Hope this may help you
Bookmarks