【问题标题】:C++ Custom exception derived from std::exception not being caught未捕获从 std::exception 派生的 C++ 自定义异常
【发布时间】:2015-12-22 16:33:10
【问题描述】:

我正在使用以下代码编写自定义异常类:

#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 &amp;exc) 块中捕获我自己的异常。它只被catch (...) 块捕获。

由于MyException 是从std::exception 继承的,我想它会被第一个catch 块捕获...为什么没有发生?

原码链接here

【问题讨论】:

  • 您已经编写了太多 Java(或 C# 或其他)。放弃new - 你正在抛出MyException*
  • 请注意,您从what 返回的指针在函数返回后立即失效且无法使用。
  • 多么悲惨的声明new...感谢您的帮助。
  • 如果您是从 Java 背景开始接触 C++,那么现在就是尝试忘记它的好时机。
  • 除非确实需要,否则不要使用std::endl'\n' 结束一行,没有刷新流的开销。

标签: c++ c++11 exception


【解决方案1】:

这并不能直接回答问题,但它非常重要

这个函数是一个等待发生的不安全崩溃:

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 字符串。

当你编写这样的异常类时,必须将完整的错误消息存储在异常中 - 在构造函数中构建它。

这是一个安全的替代品:

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
}

【讨论】:

  • 我实际上建议从logic_errorruntime_error 派生;那些具有内置的字符串处理支持(并且不要复制)。
  • 完全同意。这篇文章只是关于防止当前设计中的未定义行为。
【解决方案2】:

这个:

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

应该是:

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

否则您需要通过指针来捕捉,这不是标准做法。您当前的 catch by const-reference 是惯用且正确的——您只需要直接抛出异常而不是动态分配。

【讨论】:

    【解决方案3】:

    按值抛出:

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

    从技术上讲,您可以通过指针捕获 new'ed 异常,但
    不要这样做

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

    更多详情请见What should I throw/catch?

    Alexandrescu/Sutter's,C++ 编码标准:101 条规则...,规则 73:

    按值抛出,按引用捕获

    【讨论】:

    • 请不要写“不推荐”——这听起来仍然像是“不常见,但你可以……”。扔指针是不行的。
    • 我想说粗体字清楚地表明,虽然有可能你不应该这样做。
    • @ArneMertz 谢谢!我让它变得更强。如果您认为它应该更突出,请随时编辑。
    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2017-05-24
    • 2021-12-23
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多