c++如何catch到除数为0的异常
来源:百度知道 编辑:UC知道 时间:2024/06/03 05:02:17
如果我没有记错的话,这个异常用try /catch是捕获不到的
好像是被底层封装起来了.
如果有需要,应该自己写这个异常.
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Error{
public :
virtual void showErrorMsg()=0;
};
class MyExceptionForZero : public Error{
private:
string errMsg;
public :
MyExceptionForZero(string str) : errMsg(str){};
void showErrorMsg(){
cout<<errMsg<<endl;
}
};
void myTest(int i,int j) {
MyExceptionForZero m("除数为0啦");
cout<<"I want to get the result for j/i..."<<endl;
if (i==0) throw m;
else cout<<j/i<<endl;
}
int main(int argc, char *argv[])
{
int i=0;