如何在 Visual C++ 非托管代码中编写用户定义的异常?

1 c++ exception-handling unmanaged exception visual-c++

我想知道如何在 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)
//  { }
};
Run Code Online (Sandbox Code Playgroud)

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

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

有人可以帮帮我吗?

Mar*_*som 5

派生自std::exception而不是Exception

更好的是从stdexcept.

  • 另外,一定要`throw MyException(foo);`,而不是`throw new MyException(foo);` (3认同)