简体   繁体   English

源自std :: exception的C ++自定义异常未被捕获

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

I´m writing a custom exception class with the following code: 我正在使用以下代码编写自定义异常类:

#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;
    }
}

It compiles fine, but I cannot catch my own exception from the catch (const std::exception &exc) block. 它编译得很好,但我无法从catch (const std::exception &exc)块中捕获我自己的异常。 It is only caught by the catch (...) block. 它只能被catch (...)catch (...)

As MyException is inherited from std::exception I supposed it was gonna be caught by the first catch block... Why is that not happening ? 由于MyException是从std::exception继承的,我认为它会被第一个catchcatch ...为什么不发生?

Original code link here 原始代码链接在这里

Throw by value: 按价值投掷:

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

Technically, you could catch the new 'ed exception by pointer, but 从技术上讲,你可以通过指针捕获new 'ed异常,但是
do not do it : 不要这样做

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

For more details, see What should I throw/catch? 有关更多详细信息,请参阅我应该扔什么/捕获?

or 要么

Alexandrescu/Sutter's, C++ Coding Standards: 101 Rules... , rule 73: Alexandrescu / Sutter, C ++编码标准:101规则...... ,规则73:

Throw by value, catch by reference 按价值投掷,以参考方式捕获

This does not directly answer the question, but it's really important 这并没有直接回答这个问题,但这非常重要

this function is an unsafe crash waiting to happen: 这个函数是一个等待发生的不安全崩溃:

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() is returning the c-string inside the temporary string called whatString . string::c_str()返回名为whatString的临时字符串中的c-string。

When you write an exception class like this, you must store the complete error message in the exception - build it in the constructor. 编写这样的异常类时,必须将完整的错误消息存储在异常中 - 在构造函数中构建它。

here's a safe replacement: 这是一个安全的替代品:

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

Also, when you re-throw, don't do this: 此外,当你重新投掷时,不要这样做:

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

do this instead: 改为:

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

This: 这个:

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

Should be: 应该:

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

Otherwise you'd need to catch by pointer, which isn't standard practice. 否则你需要通过指针捕获,这不是标准做法。 Your current catch by const-reference is idiomatic and correct--you just need to throw the exception directly rather than dynamically allocating. const-reference的当前catch是惯用的和正确的 - 你只需要直接抛出异常而不是动态分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM