I have a very simple question. Would really appreciate if a C++ programmer can guide me. I want to write the C# code below in C++ dll. Can you please guide?
C# code to be translated:
void someMethod{
try
{
//performs work, that can throw an exception
}
catch(Exception ex)
{
Log(ex.Message);//logs the message to a text file
}
}
//can leave this part, i can implement it in C++
public void Log(string message)
{
//logs message in a file...
}
I have already done something similar in C++ but I can't get the message part of try{}catch(...).
You may probably want to catch all the exceptions thrown.
So add catch all (catch(…)) also for that:
The reason you can't get the exception with:
is because you aren't declaring the exception variable in the catch block. That would be the equivalent of (in C#):
You can get the exception with the following code:
Try:
But use exceptions with care. Exceptions in C++
I assume that the requested function is exported by the DLL, so I prevent any flying exception.