简体   繁体   English

使用时 <exception> 类中,what()函数似乎无法正常工作

[英]When using the <exception> class, the what() function seems to not be working properly

in this particular program I implemented a deque. 在此特定程序中,我实施了双端队列。 the only problem seems to be my error handling. 唯一的问题似乎是我的错误处理。 here is the relevant code: 这是相关代码:

#include <exception>

class Deque {
    public:
        class RemoveError : exception {
            public:
                 virtual const char* what() const noexcept {
                     return "error: cannot remove element; the queue is empty";
            }
        }R_error;

        T removeFirst() throw(RemoveError);
};

Deque::removeFirst() throw(RemoveError) {
    if (isEmpty()) 
        { throw R_error; }
}

int main () {
    try {
        Deque Q;
        printf ( "this should be an error :%d\n", Q.removeFirst ( ) );
    }
    catch(exception& e) {
        printf ( e.what(), '\n');
    }
}

and yet, when I deliberately try to remove one too many nodes from my deque, the error on the screen is 但是,当我故意从双端队列中删除一个节点过多时,屏幕上的错误是

terminate called after throwing an instance of 'Deque::RemoveError 引发'Deque :: RemoveError实例后终止调用

instead of 代替

error: cannot remove element; 错误:无法删除元素; the queue is empty 队列是空的

I have tried both using and not using the virtual in my what() function, and I tried throwing RemoveError() instead of R_error . 我已经尝试在what()函数中同时使用和不使用virtual函数,并且尝试抛出RemoveError()而不是R_error

is there any reason this is happening? 这有什么原因吗? I tried to find an answer on here but couldn't find this particular problem. 我试图在此处找到答案,但找不到此特定问题。

edit: as noted below, I had my exceptions private; 编辑:如下所述,我的例外是私人的; changing 改变中

class RemoveError : exception {

to either

struct RemoveError : exception {
class RemoveError : public exception {

fixed it. 修复。 Thanks everyone for your help 谢谢大家的帮助

That error message gets produced by g++ default uncaught exception handler, see Verbose Terminate Handler : 该错误消息由g ++默认未捕获的异常处理程序产生,请参见Verbose Terminate Handler

The __verbose_terminate_handler function obtains the name of the current exception, attempts to demangle it, and prints it to stderr. __verbose_terminate_handler函数获取当前异常的名称,尝试对其进行解密,然后将其打印到stderr。 If the exception is derived from exception then the output from what() will be included. 如果异常是从异常派生的,则将包括what()的输出。

But in: 但在:

class RemoveError : exception

std::exception is not a public base class of RemoveError , hence that exception handler cannot call its what function. std::exception不是RemoveError公共基类,因此该异常处理程序无法调用其what函数。

Fix: 固定:

class RemoveError : public exception

Or: 要么:

struct RemoveError : exception

Note 1: 注1:

virtual const char* what() const noexcept

Should be: 应该:

const char* what() const noexcept override

Note 2: 笔记2:

T removeFirst() throw(RemoveError);

Exception specification is depricated in C++11. C ++ 11中描述了异常规范。 That should be just plain: 那应该很简单:

T removeFirst();

terminate called after throwing an instance of 'Deque::RemoveError' 抛出'Deque :: RemoveError'实例后调用终止

std::terminate was called as there was an active exception that, through stack unwinding, was not caught. 之所以调用std::terminate是因为存在一个活动异常,该异常通过堆栈展开没有被捕获。
In order to retrieve the message thrown along with the exception you have to catch the exception somewhere and call what . 为了检索与异常一起引发的消息,您必须在某个地方捕获异常并调用what

{
    try {
        // code that might throw
    } catch(RemoveError e) {
       std::cerr << e.what() << std::endl;
    }
}

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

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