I read about unverified and verified questions, none of the online resources were really clear about the difference and when to use both.
From what I understand, both of them fall at runtime, both of them represent program states that are outside the expected boundaries of the logic, but checked exceptions should be clearly caught, but not marked - not.
My question is: suppose for an argument I have a method that divides two numbers
double divide(double numerator, double denominator) { return numerator / denominator; }
and a method requiring somewhere divison
void foo() { double a = divide(b, c); }
Who is responsible for verifying that the value of the denominator is zero, and whether or not to exclude (by ignoring Java's built-in division check)?
So whether the divide method will be declared as is or how
double divide(double numerator, double denominator) throws DivideByZeroException { if(denominator == 0) throw DivideByZeroException else ... } void foo() { try{ double a = divide(b, c); } catch(DivideByZeroException e) {} }
or without a checked exception, as is:
double divide(double numerator, double denominator) { if(denominator == 0) throw DivideByZeroException else ... } void foo() { if(c != 0) double a = divide(b, c); }
and let foo make the division a null check?
This problem originally arose in a mathematical program that I wrote, in which users entered numbers and logical classes performed by calculations. I was never sure if the GUI should immediately check for incorrect values, or if the internal logic should catch them during the calculation and throw exceptions.
java checked exception throw unchecked
donnyton
source share