从 std::exception 派生的 C++ 自定义异常未被捕获

标签 c++ c++11 exception

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

#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) block 中捕获我自己的异常。它仅被 catch (...) block 捕获。

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

原码链接here

最佳答案

这并没有直接回答问题,但它非常重要

此函数是等待发生的不安全崩溃:

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();
}

另外,当你重新 throw 时,不要这样做:

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
}

关于从 std::exception 派生的 C++ 自定义异常未被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34420388/

相关文章:

c++ - SFINAE 用于 CRTP 结构特征的特化

java - 异常类型创建的类在另一个类中使用

c++ - 为什么此类定义中不需要空参数列表?

c++ - 具有继承类的空指针

c++ - 如何减少默认的 C++ 内存消耗?

c++ visual studio 2012 #include 不起作用

c++ - 基于范围的循环、唯一指针和 move 语义

java - java中需要抛出什么异常?

java - while 循环出现 NoSuchElementException

c++ - 复制字符串集的正确方法是什么(在复制构造函数和赋值运算符中)?