Reputation: 34760
My C# called a method from a C++ dll, after the call returned, my application just gone without any message. want to add try catch to get the reason. how can I do it. Just a try-catch in the method call?
EDIT: the [HandledProcessCorruptedStateExceptions] is not belong to C#?
Upvotes: 2
Views: 2741
Reputation: 57268
try{
return (22 / 0); // Cant devise by 0
}catch (System.DivideByZeroException ZeroEx) //Catch type of exception and assign to variable
{
Console.WriteLine("Division by zero attempted!");
Console.WriteLine("Are you sure you wish to continue");
string answer = Console.Read();
}
Like so
Upvotes: 0
Reputation: 176169
An interesting article on exception handling, also with respect to handling corrupted state exceptions:
However, I would assume that there is something wrong either in the way you are calling the native method or in the native method itself. It's best to fix the original problem that is causing the CSE instead of catching exceptions that indicate that your application is no longer in a stable state. You probably will only make things worse by catching such an expcetion. The article mentioned above states:
Even though the CLR prevents you from naively catching CSEs, it's still not a good idea to catch overly broad classes of exceptions. But catch (Exception e) appears in a lot of code, and it's unlikely that this will change. By not delivering exceptions that represent a corrupted process state to code that naively catches all exceptions, you prevent this code from making a serious situation worse.
Upvotes: 1