【发布时间】:2013-04-03 17:52:18
【问题描述】:
我的程序如下:(在 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时得到的输出如下:
administrator@ubuntu:~/FYMCA/CPP_class$ g++ prog110.cpp 管理员@ubuntu:~/FYMCA/CPP_class$ ./a.out 输入分子 和分母: 12 0 在抛出一个实例后终止调用 'char const*' 中止(核心转储)
我该如何解决这个问题?
【问题讨论】:
标签: c++