自定义异常

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class myException
{
public:
    void printError()
    {
        cout << "自定义异常" << endl;
    }
};
int myDevide(int a, int b)
{
    if (b == 0)
    {
        throw myException(); //匿名对象
    }
    return a / b;
}
void test01()
{
    int a = 10;
    int b = 0;
    try
    {
        myDevide(a, b);
    }
    catch (myException e)
    {
        e.printError();
    }
}
int main()
{
    system("Pause");
    return 0;
}

结果:

原文地址:https://www.cnblogs.com/yifengs/p/15183747.html