I want to restrict my function divide
to always be called from a try block. But when the function is called from main
, without using try block, it does not show "Unhandled Exception" error?
class Main {
public static void main(String[] args) {
System.out.println(Main.divide(5.0f, 2.0f));
System.out.println(divide(5.0f, 2.0f));
}
static float divide(float x, float y) throws ArithmeticException {
if (y == 0)
throw new ArithmeticException("Cannot divide by 0!");
else
return x/y;
}
}
Output:
2.5
2.5