Hi guys I'm trying to figure out whether it is possible to catch(or at least suppress) all kinds of unmanaged exceptions in managed code? I've seen a lot of questions here already but it is still unclear for me, what types of exceptions are catchable and what are not?
Just as an example i've made a C++ programm that performs division by zero:
printf("Hello from unmanaged code\n");
int a = 0;
printf("%d\n", 10 / a);
return 0;
And a C# application
try
{
Process p = new Process();
p.StartInfo.FileName = "test.exe";
p.Start();
p.WaitForExit();
Console.WriteLine("success");
}
catch (Win32Exception)
{
Console.WriteLine("1");
}
catch(ExternalException)
{
Console.WriteLine("2");
}
catch
{
Console.WriteLine("3");
}
None of the catchs here does not trigger, program termintaion window appeares and after closing it, C# programm continues normal execution.
无论使用何种语言或使用catch块, 您都无法处理来自不同进程的异常 。
As mentioned by nvoigt you can not handle exception in this case. a more suitable solution for this case is reading the returned value from C++ code. for example : if (a != 0) return 1; then handling it in your C# code like: if (p.ExitCode == 1) Console.WriteLine("division by zero");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.