📜  C ++中带有类的用户定义的自定义异常

📅  最后修改于: 2021-05-31 18:27:14             🧑  作者: Mango

我们也可以在类中使用异常处理。甚至我们可以抛出用户定义的类类型的异常。为了在try块中抛出say demo类类型的异常,我们可以编写

throw demo();

示例1:使用单个类实现异常处理的程序

CPP14
#include 
using namespace std;
 
class demo {
};
 
int main()
{
    try {
        throw demo();
    }
 
    catch (demo d) {
        cout << "Caught exception of demo class \n";
    }
}


CPP14
#include 
using namespace std;
 
class demo1 {
};
 
class demo2 {
};
 
int main()
{
    for (int i = 1; i <= 2; i++) {
        try {
            if (i == 1)
                throw demo1();
 
            else if (i == 2)
                throw demo2();
        }
 
        catch (demo1 d1) {
            cout << "Caught exception of demo1 class \n";
        }
 
        catch (demo2 d2) {
            cout << "Caught exception of demo2 class \n";
        }
    }
}


CPP14
#include 
using namespace std;
 
class demo1 {
};
 
class demo2 : public demo1 {
};
 
int main()
{
    for (int i = 1; i <= 2; i++) {
        try {
            if (i == 1)
                throw demo1();
 
            else if (i == 2)
                throw demo2();
        }
 
        catch (demo1 d1) {
            cout << "Caught exception of demo1 class \n";
        }
        catch (demo2 d2) {
            cout << "Caught exception of demo2 class \n";
        }
    }
}


CPP14
#include 
using namespace std;
 
class demo {
    int num;
 
public:
    demo(int x)
    {
        try {
 
            if (x == 0)
                // catch block would be called
                throw "Zero not allowed ";
 
            num = x;
            show();
        }
 
        catch (const char* exp) {
            cout << "Exception caught \n ";
            cout << exp << endl;
        }
    }
 
    void show()
    {
        cout << "Num = " << num << endl;
    }
};
 
int main()
{
 
    // constructor will be called
    demo(0);
    cout << "Again creating object \n";
    demo(1);
}


输出:

Caught exception of demo class

说明:在程序中我们声明了一个空类。在try块中,我们抛出了一个演示类类型的对象。 try块捕获对象并显示。
示例2:用两个类实现异常处理的程序

CPP14

#include 
using namespace std;
 
class demo1 {
};
 
class demo2 {
};
 
int main()
{
    for (int i = 1; i <= 2; i++) {
        try {
            if (i == 1)
                throw demo1();
 
            else if (i == 2)
                throw demo2();
        }
 
        catch (demo1 d1) {
            cout << "Caught exception of demo1 class \n";
        }
 
        catch (demo2 d2) {
            cout << "Caught exception of demo2 class \n";
        }
    }
}

输出:

Caught exception of demo1 class
Caught exception of demo2 class

具有继承的异常处理:

异常处理也可以在继承的帮助下实现。在继承的情况下,派生类抛出的对象被第一个catch块捕获。
例子:

CPP14

#include 
using namespace std;
 
class demo1 {
};
 
class demo2 : public demo1 {
};
 
int main()
{
    for (int i = 1; i <= 2; i++) {
        try {
            if (i == 1)
                throw demo1();
 
            else if (i == 2)
                throw demo2();
        }
 
        catch (demo1 d1) {
            cout << "Caught exception of demo1 class \n";
        }
        catch (demo2 d2) {
            cout << "Caught exception of demo2 class \n";
        }
    }
}

输出:

Caught exception of demo1 class
Caught exception of demo1 class

说明:程序类似于以前之一,但在这里我们已经取得DEMO2demo1的。注意派生类demo1的catch块被写入first.As demo1的DEMO2抛出DEMO2类的一个对象的基类将首先捕捉处理这就是为什么输出如图所示。

构造函数的异常处理:

异常处理也可以通过使用构造函数来实现。虽然我们不能从构造函数中返回任何值,但是在try and catch块的帮助下我们可以。
例子:

CPP14

#include 
using namespace std;
 
class demo {
    int num;
 
public:
    demo(int x)
    {
        try {
 
            if (x == 0)
                // catch block would be called
                throw "Zero not allowed ";
 
            num = x;
            show();
        }
 
        catch (const char* exp) {
            cout << "Exception caught \n ";
            cout << exp << endl;
        }
    }
 
    void show()
    {
        cout << "Num = " << num << endl;
    }
};
 
int main()
{
 
    // constructor will be called
    demo(0);
    cout << "Again creating object \n";
    demo(1);
}

输出:

Exception caught
 Zero not allowed
Again creating object
Num = 1

说明:x = 0时引发异常调用catch块。当x = 1时,不会创建异常。

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”