如何打印捕获的异常消息?
我有一个非常简单的问题。如果 C++ 程序员可以指导我,我将非常感激。我想在 C++ dll 中编写下面的 C# 代码。您能指导一下吗?
要翻译的 C# 代码:
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...
}
我已经在 C++ 中做了类似的事情,但我无法获取 try{}catch(...) 的消息部分。
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(...).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
但是要小心使用例外。 C++ 中的异常
But use exceptions with care. Exceptions in C++
您可能想捕获所有抛出的异常。
因此还为此添加 catch all (catch(...)) :
You may probably want to catch all the exceptions thrown.
So add catch all (catch(…)) also for that:
尝试:
Try:
无法使用以下方法获得异常的原因
是因为您没有在 catch 块中声明异常变量。这相当于(在 C# 中):
您可以使用以下代码获取异常:
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:
我假设所请求的函数是由 DLL 导出的,因此我可以防止出现任何异常。
I assume that the requested function is exported by the DLL, so I prevent any flying exception.