all groups > dotnet clr > august 2006 >
You're in the

dotnet clr

group:

Catching managed exceptions in unmanaged code


Catching managed exceptions in unmanaged code Erlend
8/11/2006 7:02:23 AM
dotnet clr:
Hello,

I need to be able to catch clr exceptions in unmanaged code. Anyone
know how to do that?

I'm able to catch native types, so there is obviously some marshalling
going on, but I don't know what type the Exception^ is cast to. Any
help will be greatly appreciated!

The following example illustrates my problem:

//Compile with /clr
#include "stdafx.h"
#include <iostream>
#include <stdexcept>

using namespace System;

class Interop
{
public:
static void Managed_stde() {throw std::runtime_error("std
exception");}
static void Managed_clre() {throw gcnew ApplicationException("clr
exception");}

static void Unmanaged();
};

int main(array<System::String ^> ^args)
{
Interop::Unmanaged();

return 0;
}

#pragma unmanaged

void Interop::Unmanaged()
{
try
{
Managed_stde();
}
catch(std::exception& e)
{
std::cout << e.what() << std::endl;
}

try
{
Managed_clre();
}
catch(...) // Can only catch all to trap the clr exception!
{
std::cout << "unknown exception" << std::endl;
}
}
Re: Catching managed exceptions in unmanaged code Barry Kelly
8/11/2006 4:15:13 PM
[quoted text, click to view]

I think it would probably be easiest to write a wrapper managed function
that does the catch, and marshal the managed exception manually to your
unmanaged code.

-- Barry

--
Re: Catching managed exceptions in unmanaged code Vadym Stetsyak
8/11/2006 5:54:18 PM
SGVsbG8sIEVybGVuZCENCg0KSXMgaXQgaW1wb3J0YW50IGZvciB5b3UgdG8gY29tcGlsZSB2b2lk
IEludGVyb3A6OlVubWFuYWdlZCgpIGFzIHVubWFuYWdlZD8NCklmIG5vdCB5b3UgY2FuIHJlbW92
ZSAjcHJhZ21hIHVubWFuYWdlZCBkaXJlY3RpdmUgYW5kIGFkZCBhbm90aGVyIGNhdGNoLg0KDQou
Li4NCnRyeQ0KICB7DQogICAgTWFuYWdlZF9jbHJlKCk7DQogIH0NCiAgY2F0Y2goRXhjZXB0aW9u
IF5leCkgIC8vIENhbiBvbmx5IGNhdGNoIGFsbCB0byB0cmFwIHRoZSBjbHIgZXhjZXB0aW9uIQ0K
ICB7DQogICAgICAgIENvbnNvbGU6OldyaXRlTGluZShleC0+TWVzc2FnZSk7DQogICB9DQouLi4N
Cg0KLS0NClJlZ2FyZHMsIFZhZHltIFN0ZXRzeWFrDQp3d3c6IGh0dHA6Ly92YWRteXN0LmJsb2dz
cG90LmNvbQ==
Re: Catching managed exceptions in unmanaged code Erlend
8/14/2006 12:52:04 AM
Yes, unfortunately it needs to be unmanaged.

We have a large legacy application written in MFC/c++. We provide an
API for our clients so they can make plug-in modules. This API is also
available in .NET. for writing managed plug-in. So, we need to handle
managed exceptions in our API code, and obviously in our clients
plug-in code.

I think its a bit strange the IJW does not handle this case. I think
the natural way would be to marshal the Exception class into the
<stdexcept> model.

Erlend

[quoted text, click to view]
Re: Catching managed exceptions in unmanaged code Erlend
8/14/2006 12:54:40 AM
Yes, at the moment it looks like that is the solution. But we have some
200 functions in our API so if we could avoid doing the marshalling
manually, that would be the best.

Erlend

[quoted text, click to view]
AddThis Social Bookmark Button