Handling Arithmetic Exceptions

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


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);

Leave a Reply

Your email address will not be published. Required fields are marked *