Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling exceptions in C++/CLI

In unmanaged C++ the current thinking is to always catch exceptions via const reference.

I'm not that experienced with managed C++ (although I'm getting to grips with the differences) but I'm wondering, what is the best way to catch exceptions here? Would exceptions ever be caught by handle i.e.

try
{
}
catch( ExceptionType^ ex )
{
}

.. and if so, would there be any caveats?

like image 911
Konrad Avatar asked Jan 28 '26 12:01

Konrad


2 Answers

For handling managed code exceptions, yes that's the way to do it.

However if you are using Managed C++, I imagine this is becase you have the need to interop with native code (or else you are just a masochist? sorry, I am no fan of Managed C++), in which case things are more complicated. See here for some background on mixed-mode exception handling issues.

like image 185
Steve Townsend Avatar answered Jan 30 '26 08:01

Steve Townsend


When you throw a managed exception, then there is no way to catch it except by garbage-collected handle. Managed code has much stricter rules about what can and cannot be thrown, unlike C++.

like image 30
Puppy Avatar answered Jan 30 '26 08:01

Puppy