I am forced to use CLI because of some C# dependencies in my C++ code. Before this dependency came in, I wrote an exception, which inherits from std::exception. Whenever I throw this exception now, my program crashes with an access violation exception comming from ntd.dll.
So I put the header file, which contains the exception in a new CLI project and tried to compile it. This gave me the error, that "exception" is not a member of std. After including <exception>
, this error was gone (of course), but I wonder, why this wasn't necessary before..
Anyways, here's my code in the basic example:
The exception header:
#pragma once
#include <exception>
//Device is offline
struct E_DvcOffline : public std::exception
{
const char * what() const throw ()
{
return "The Device is offline";
}
};
The main function:
#include <MyExceptions.hpp>
#include <iostream>
using namespace System;
int main(array<String^>^ args) {
try {
throw E_DvcOffline();
}
catch (E_DvcOffline) {
std::cout << "Caught it" << std::endl;
std::cin.get();
}
}
And the exception I receive, when the code throws my custom exception:
Not able to embed pictures yet...
Thanks in advance, Calvin
EDIT
throw new E_DvcOffline();
changed to throw E_DvcOffline();
I was able to fix the problem.
#pragma once
**#pragma managed(push, off)**
#include <exception>
//Device is offline
struct E_DvcOffline : public std::exception
{
const char * what() const throw ()
{
return "The Device is offline";
}
};
**#pragma managed(pop)**
Changes are bolt. I didn't search for it, but I think, that this tells the compiler to treat this code as unmanaged and therefore calls the native exception handler.
Warning: Visual Studio will eventually yield the same error as before, when you go through the code step by step in debug mode. Otherwise everything works fine.