我的程序如下:(在linux上)
// Ex. 2 of Exception Handling
// Here divn() raises the exception but main() will be the exception handler
#include<iostream>
using namespace std;
int divn(int n, int d)
{
if(d == 0)
throw "Division by ZERO not possible ";
return n/d;
}
main()
{
int numer, denom;
cout << "Enter the numerator and denominator : ";
cin >> numer >> denom;
try
{
cout << numer << " / " << denom << " = " << divn(numer,denom) << endl;
}
catch(char *msg)
{
cout << msg;
}
cout << "\nEnd of main() \n ";
}
/*当我们将分母设为0时,它应该抛出异常并提供给定的错误消息。当我输入分母为0时得到的输出如下:
管理员@ubuntu:~/FYMCA/CPP_class$ g++ prog110.cpp administrator@ubuntu:~/FYMCA/CPP_class$ ./a.out 输入分子和分母: 12 0 在抛出 'char const*' 实例后终止调用已中止(核心转储)
我该如何解决这个问题?