Can you catch a native exception in C# code?
You can use Win32Exception and use its NativeErrorCode property to handle it appropriately.
// http://support.microsoft.com/kb/186550
const int ERROR_FILE_NOT_FOUND = 2;
const int ERROR_ACCESS_DENIED = 5;
const int ERROR_NO_APP_ASSOCIATED = 1155;
void OpenFile(string filePath)
{
Process process = new Process();
try
{
// Calls native application registered for the file type
// This may throw native exception
process.StartInfo.FileName = filePath;
process.StartInfo.Verb = "Open";
process.StartInfo.CreateNoWindow = true;
process.Start();
}
catch (Win32Exception e)
{
if (e.NativeErrorCode == ERROR_FILE_NOT_FOUND ||
e.NativeErrorCode == ERROR_ACCESS_DENIED ||
e.NativeErrorCode == ERROR_NO_APP_ASSOCIATED)
{
MessageBox.Show(this, e.Message, "Error",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
}
catch native exception in dotnet core
I set-up this Minimal, Complete and Verifiable example that demonstrates how native exceptions are NOT caught whatsoever by managed C# .NET Core code, on Linux.
As described in the issue I opened for dotnet/coreclr
, I've tried (m)any possible weapon(s) in the arsenal, to no avail.
The direct answer given by dotnet team was:
We do not support exception handling interop on Unix. There is no good way to do it. The Mono project has a great write up on it here: http://www.mono-project.com/docs/advanced/pinvoke/#runtime-exception-propagation . The same reasoning applies to .NET Core.
The Mono project's solution, which is also recommended by the dotnet team, is:
C++ exceptions will need to be mapped into an “out” parameter or a return value, so that managed code can know what error occurred, and (optionally) throw a managed exception to “propagate” the original C++ exception.
That's what we eneded up implementing and, well, it works :).
Capturing native exception from c# and getting the exception.what() after it leaves scope
You could allocate a char-buffer (within the native DLL) for your "errorText"-Parameter and copy the text "ex.what()" to that buffer. In that case, the Memory will stay valid.
But I think, you will have to release the char-buffer by your own after you read the string in C# to prevent a memory-leak.
Related Topics
Casting a Variable Using a Type Variable
Type or Namespace Name Does Not Exist
Generating Permutations of a Set (Most Efficiently)
Populating a Razor Dropdownlist from a List<Object> in MVC
How to Restart My C# Winform Application
Calling a Method Every X Minutes
How to Read the Color of a Screen Pixel
C# Open a New Form Then Close the Current Form
How to Make an Event in the Usercontrol and Have It Handled in the Main Form
Wcf Named Pipe Minimal Example
Call Signalr Core Hub Method from Controller
Export Datatable to Excel with Epplus
What Is the Best Data Type to Use for Money in C#
Shorter Syntax for Casting from a List<X> to a List<Y>
How to "Steal" an Event Handler from One Control and Give It to Another
Do You Have to Put Task.Run in a Method to Make It Async
Is It Better to Create a Singleton to Access Unity Container or Pass It Through the Application
How to Use Webrequest to Access an Ssl Encrypted Site Using Https