Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Arithmetic Exceptions

Tags:

java

exception

I've created my own exception to handle situations like arithmetic exceptions, and other situation involving mathematic rules. But When I call it never goes to my Exception for example,on division by zero goes to arithmetic exception instead

like image 613
alculete Avatar asked Dec 09 '22 07:12

alculete


1 Answers

Division by zero and other "standard" arithmetic errors are handled by the runtime or the class library, which don't know about your user defined exception. You can only use your own exceptions in your own code by explcitly throwing them when it is appropriate.

Of course, it is possible to catch any arithmetic exceptions thrown by the class library and wrap them into your own exceptions:

try {
  ...
} catch (java.lang.ArithmeticException exc) {
  throw new MyException("An arithmetic error occurred", exc);
}
like image 161
Péter Török Avatar answered Dec 27 '22 05:12

Péter Török