【问题标题】:How to write user defined exceptions in Visual C++ unmanaged code?如何在 Visual C++ 非托管代码中编写用户定义的异常?
【发布时间】:2012-05-24 08:09:31
【问题描述】:

我想知道如何在 Visual C++ 中编写非托管异常?

#include <string>
#include <exception>

using namespace std;
using namespace System;

class GraphException : public Exception
{
public:
    GraphException() { }
//  GraphException(string message) : Exception (message)
//  { }
//  GraphException(string message, Exception inner) : Exception (message, inner)
//  { }
};

这不起作用,我收到以下错误,

错误 1 ​​错误 C3625:“GraphException”:无法派生非托管类型 从托管类型 'System::Exception' c:\breadthfirst\graph\graphexception.h 10 1 广度优先

有人可以帮帮我吗?

【问题讨论】:

    标签: c++ visual-c++ exception exception-handling unmanaged


    【解决方案1】:

    派生自 std::exception 而不是 Exception

    更好的是从stdexcept 中更专业的异常之一派生出来。

    【讨论】:

    • 顺便说一句,你可以throw 任何你喜欢的东西。它可以是您喜欢的任何类/结构......甚至是原始类型。
    • 另外,一定要throw MyException(foo);,而不是throw new MyException(foo);
    • @Kathy 另外,请注意 VC++ 中的std::exception 定义了几个非标准构造函数。唯一的标准构造函数是exception() noexcept 和复制构造函数。 VC++定义了一个const char *成员变量来指向异常的原因,这也不是C++标准的一部分。
    猜你喜欢
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2021-05-12
    • 2010-12-29
    相关资源
    最近更新 更多