C++ 拋異常處理

一、概念

C++ 標準的異常

C++ 提供了一系列標準的異常,定義在 <exception> 中,我們可以在程序中使用這些標準的異常。它們是以父子類層次結構組織起來的,如下所示:

C++ 異常的層次結構

下表是對上面層次結構中出現的每個異常的說明:

異常 描述
std::exception 該異常是所有標準 C++ 異常的父類。
std::bad_alloc 該異常可以通過 new 拋出。
std::bad_cast 該異常可以通過 dynamic_cast 拋出。
std::bad_exception 這在處理 C++ 程序中無法預期的異常時非常有用。
std::bad_typeid 該異常可以通過 typeid 拋出。
std::logic_error 理論上可以通過讀取代碼來檢測到的異常。
std::domain_error 當使用了一個無效的數學域時,會拋出該異常。
std::invalid_argument 當使用了無效的參數時,會拋出該異常。
std::length_error 當創建了太長的 std::string 時,會拋出該異常。
std::out_of_range 該異常可以通過方法拋出,例如 std::vector 和 std::bitset<>::operator[]()。
std::runtime_error 理論上不可以通過讀取代碼來檢測到的異常。
std::overflow_error 當發生數學上溢時,會拋出該異常。
std::range_error 當嘗試存儲超出範圍的值時,會拋出該異常。
std::underflow_error 當發生數學下溢時,會拋出該異常。

定義新的異常

您可以通過繼承和重載 exception 類來定義新的異常。下面的實例演示瞭如何使用 std::exception 類來實現自己的異常:

實例

#include "pch.h"
#include <iostream>
#include <stdexcept>
#include <exception>
#include <fstream>
#include <vector>

using namespace std;

struct MyException :public exception
{
    const char *what() const throw()
    {
        return "C++ Exception";
    }
};
int main()
{    
    try
    {
        throw MyException();
    }
    catch (MyException &e)
    {
        cout << "MyException caught" << endl;
        cout << e.what() << endl;
    }
    catch (exception &e)
    {
        //其他的錯誤
    }
    return 0;
}

這將產生以下結果:

MyException caught
C++ Exception

在這裏,what() 是異常類提供的一個公共方法,它已被所有子異常類重載。這將返回異常產生的原因。

二、舉例


#include "pch.h"
#include <iostream>
#include <stdexcept>
#include <exception>
#include <fstream>
#include <vector>

using namespace std;

void readIntegerFile(const string &fileName, vector<int> &dest)
{
    ifstream istr;
    int temp;
    istr.open(fileName);
    if (istr.fail())
    {
        throw invalid_argument("Unable to open the file");
    }
    while (istr >> temp)
    {
        dest.push_back(temp);
    }
    if (!istr.eof())
    {
        throw runtime_error("Error reading the file.");
    }
}


int main()
{
    vector <int> myInts;
    const string &fileName = "E:/test.txt";

    try
    {
        readIntegerFile(fileName, myInts);
    }
    catch (const runtime_error& e) {
        cerr << e.what() << endl;
        return 1;
    }
    catch (const invalid_argument& e) {
        cerr << e.what() << endl;
        return 1;
    }

    for (const auto&element : myInts)
    {
        cout << element << " ";
    }

    cout << endl;
    
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章