Originally Posted by
vikkyhacks
In C++ we have operator overloading to change the operator's behavior at runtime, any such modifications or ideas such that the "/" throws myException (the exception that I wrote) in these case ??? If it can throw an arithmetic exception then there should be a way to throw user defined exceptions ....
Java does not have operator overloading. You cannot change the behaviour of /
Options:
1.
Code:
if (n2 == 0) {
throw new MyException();
}
System.out.println(MyArithmetic.integerDivide(n1, n2));
2.
Code:
try {
System.out.println(n1 / n2);
}
catch (java.lang.ArithmeticException arithmeticException) {
throw new MyException();
}
3. Replace the operator with some class & function which avoids or translates the arithmeticException as above.
Code:
System.out.println(MyArithmetic.integerDivide(n1, n2));
Bookmarks