簡體   English   中英

源自std :: exception的C ++自定義異常未被捕獲

[英]C++ Custom exception derived from std::exception not being caught

我正在使用以下代碼編寫自定義異常類:

#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <iomanip>
#include <algorithm>    


class MyException : public std::exception {

public:
    MyException();
    explicit MyException(std::string message);
    MyException(std::string source, std::string message);
    MyException(int code, std::string source, std::string message);
    const char *what() const throw();


private:
    int exceptionCode;
    std::string exceptionSource;
    std::string exceptionMessage;
};


MyException::MyException() :
        exceptionCode(0),
        exceptionSource ("No source."),
        exceptionMessage ("No message.") {}

MyException::MyException(std::string message) :
        exceptionCode(0),
        exceptionSource ("No source."),
        exceptionMessage (std::move(message)) {}

MyException::MyException(std::string source, std::string message) :
        exceptionCode(0),
        exceptionSource (std::move(source)),
        exceptionMessage (std::move(message)) {}

MyException::MyException(int code, std::string source, std::string message) :
        exceptionCode(code),
        exceptionSource (source),
        exceptionMessage (message) {}

const char *MyException::what() const throw()
{
    std::cout << "What:" << exceptionMessage << std::endl;

    std::stringstream s;
    s << "MyException Data:" << std::endl;
    s << "Code    : " << exceptionCode << std::endl;
    s << "Source  : " << exceptionSource << std::endl;
    s << "Message : " << exceptionMessage << std::endl;

    std::string whatString = s.str();
    return whatString.c_str();
}


void test()
{
    throw new MyException("test", "This is a test");
}


int main()
{
    try
    {
        test();
    }
    catch (const std::exception &exc)
    {
        std::cerr << "Exception detected:" << std::endl;
        std::cerr << exc.what();
        throw exc;
    }
    catch (...)
    {
        std::cerr << "An unknown exception was called." << std::endl;
        throw;
    }
}

它編譯得很好,但我無法從catch (const std::exception &exc)塊中捕獲我自己的異常。 它只能被catch (...)catch (...)

由於MyException是從std::exception繼承的,我認為它會被第一個catchcatch ...為什么不發生?

原始代碼鏈接在這里

按價值投擲:

void test()
{
    throw MyException("test", "This is a test");
}

從技術上講,你可以通過指針捕獲new 'ed異常,但是
不要這樣做

catch (const std::exception* exc) // bad practice

有關更多詳細信息,請參閱我應該扔什么/捕獲?

要么

Alexandrescu / Sutter, C ++編碼標准:101規則...... ,規則73:

按價值投擲,以參考方式捕獲

這並沒有直接回答這個問題,但這非常重要

這個函數是一個等待發生的不安全崩潰:

const char *MyException::what() const throw()
{
    std::cout << "What:" << exceptionMessage << std::endl;

    std::stringstream s;
    s << "MyException Data:" << std::endl;
    s << "Code    : " << exceptionCode << std::endl;
    s << "Source  : " << exceptionSource << std::endl;
    s << "Message : " << exceptionMessage << std::endl;

    std::string whatString = s.str();
    return whatString.c_str();
}

string::c_str()返回名為whatString的臨時字符串中的c-string。

編寫這樣的異常類時,必須將完整的錯誤消息存儲在異常中 - 在構造函數中構建它。

這是一個安全的替代品:

class MyException : public std::exception {

public:
    MyException();
    explicit MyException(const std::string& message);
    MyException(const std::string& source, const std::string& message);
    MyException(int code, const std::string& source, const std::string& message);
    const char *what() const throw();

private:
    // helper function
    static std::string make_message(int code, const std::string& source, const std::string& message);
    std::string message;
};


MyException::MyException() :
MyException(0, "No source.", "No message.") {}

MyException::MyException(const std::string& message) :
MyException(0, "No source.", std::move(message)) {}

MyException::MyException(const std::string& source, const std::string& message) :
MyException(0, std::move(source), std::move(message)) {}

MyException::MyException(int code, const std::string& source, const std::string& message) :
message(make_message(code, source, message))
{}

const char *MyException::what() const throw()
{
    // message is a class member, not a temporary
    return message.c_str();
}

std::string MyException::make_message(int code, const std::string& source, const std::string& message)
{
    std::stringstream s;
    s << "MyException Data:" << std::endl;
    s << "Code    : " << code << std::endl;
    s << "Source  : " << source << std::endl;
    s << "Message : " << message << std::endl;

    // takes a copy, returns a copy - safe!
    return s.str();
}

此外,當你重新投擲時,不要這樣做:

catch (const std::exception &exc)
{
    std::cerr << "Exception detected:" << std::endl;
    std::cerr << exc.what();
    throw exc; // <--- this is bad - you're potentially slicing!
}

改為:

catch (const std::exception &exc)
{
    std::cerr << "Exception detected:" << std::endl;
    std::cerr << exc.what();
    throw;     // <--- ok, compiler will now rethrow the complete object
}

這個:

throw new MyException("test", "This is a test");

應該:

throw MyException("test", "This is a test");

否則你需要通過指針捕獲,這不是標准做法。 const-reference的當前catch是慣用的和正確的 - 你只需要直接拋出異常而不是動態分配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM