
Patrick B. answered 03/10/19
Math and computer tutor/teacher
/* C++ */
using namespace std;
#include <iostream>
class Test
{
private:
int A;
int B;
public:
Test ( int a, int b)
{
A=a; B=b;
}
double Divide()
{
double dblReturn;
if (B==0)
{
dblReturn = 0;
throw (" Divide by Zero exception");
}
else
{
dblReturn = (double)(A*1.0f)/B;
}
return(dblReturn);
}//Divide
};
int main()
{
int a,b;
a=b=1;
while (!((a==0) && (b==0)) )
{
std::cout << "Input the numerator and denominator separated by a space : zeros to quit :> ";
std::cin >> a >> b;
Test * myTest;
myTest = new Test(a,b);
try
{
std::cout << "Quotient is " << myTest->Divide() << endl;
}
catch (char * str)
{
std:cerr << str << endl;
}
delete(myTest);
} //while
}
//************* Java *****************//
import java.io.*;
class TestException
{
private double A;
private double B;
public TestException( double a, double b)
{
A=a; B=b;
}
public double Divide() throws ArithmeticException
{
double dblReturn;
if (B==0)
{
dblReturn=0;
throw(new ArithmeticException("Divide by zero"));
}
else
{
dblReturn = ( (double) (A*1.0f)/B);
}
return(dblReturn);
}
public static void main(String args[])
{
double a,b;
a=b=1;
Console console = System.console();
while (! ((a==0) && (b==0)))
{
System.out.print(" Input numerator : zero to quit :>");
String inbuff = console.readLine();
a = Double.parseDouble(inbuff);
System.out.print(" Input denominator : zero to quit :>");
inbuff = console.readLine();
b = Double.parseDouble(inbuff);
TestException myTest = new TestException(a,b);
try
{
double dblResult = myTest.Divide();
System.out.println(" Quotient is " + dblResult);
}
catch (ArithmeticException ex) { System.out.println(" Division by zero "); }
} //while
} //main
} //class

Patrick B.
In C++ the exception terminates execution, but in Java the loop continues03/10/19