如何打印捕获的异常消息?

发布于 2024-10-21 10:34:56 字数 474 浏览 0 评论 0原文

我有一个非常简单的问题。如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

硬不硬你别怂 2024-10-28 10:34:56
void someMethod{
//performs work
try
{}
catch(std::exception& ex)
{
    //Log(ex.Message);//logs the message to a text file
    cout << ex.what(); 
}
catch(...)
{
    // Catch all uncaught exceptions 
}

但是要小心使用例外C++ 中的异常

void someMethod{
//performs work
try
{}
catch(std::exception& ex)
{
    //Log(ex.Message);//logs the message to a text file
    cout << ex.what(); 
}
catch(...)
{
    // Catch all uncaught exceptions 
}

But use exceptions with care. Exceptions in C++

最美不过初阳 2024-10-28 10:34:56

您可能想捕获所有抛出的异常。
因此还为此添加 catch all (catch(...)) :

try
{
   // ...
}
catch(const std::exception& ex)
{
   std::cout << ex.what() << std::endl;
}
catch(...)
{
   std::cout << "You have got an exception,"
                "better find out its type and add appropriate handler"
                "like for std::exception above to get the error message!" << std::endl;
}

You may probably want to catch all the exceptions thrown.
So add catch all (catch(…)) also for that:

try
{
   // ...
}
catch(const std::exception& ex)
{
   std::cout << ex.what() << std::endl;
}
catch(...)
{
   std::cout << "You have got an exception,"
                "better find out its type and add appropriate handler"
                "like for std::exception above to get the error message!" << std::endl;
}
晨与橙与城 2024-10-28 10:34:56

尝试:

#include <exception.h>
#include <iostream>
void someMethod() {
    //performs work
    try {

    }
    catch(std::exception ex) {
        std::cout << ex.what() << std::endl;
    }
}

Try:

#include <exception.h>
#include <iostream>
void someMethod() {
    //performs work
    try {

    }
    catch(std::exception ex) {
        std::cout << ex.what() << std::endl;
    }
}
茶底世界 2024-10-28 10:34:56

无法使用以下方法获得异常的原因

try
{
}
catch (...)
{
}

是因为您没有在 catch 块中声明异常变量。这相当于(在 C# 中):

try
{
}
catch
{
    Log(ex.Message); // ex isn't declared
}

您可以使用以下代码获取异常:

try
{
}
catch (std::exception& ex)
{
}

The reason you can't get the exception with:

try
{
}
catch (...)
{
}

is because you aren't declaring the exception variable in the catch block. That would be the equivalent of (in C#):

try
{
}
catch
{
    Log(ex.Message); // ex isn't declared
}

You can get the exception with the following code:

try
{
}
catch (std::exception& ex)
{
}
淡笑忘祈一世凡恋 2024-10-28 10:34:56

我假设所请求的函数是由 DLL 导出的,因此我可以防止出现任何异常。

#include <exception.h>

// some function exported by the DLL
void someFunction() 
{
    try {
        // perform the dangerous stuff
    } catch (const std::exception& ex) {
        logException(ex.what());
    } catch (...) {
        // Important don't return an exception to the caller, it may crash
        logException("unexpected exception caught");
    }
}

/// log the exception message
public void logException(const char* const msg)
{
    // write message in a file... 
}

I assume that the requested function is exported by the DLL, so I prevent any flying exception.

#include <exception.h>

// some function exported by the DLL
void someFunction() 
{
    try {
        // perform the dangerous stuff
    } catch (const std::exception& ex) {
        logException(ex.what());
    } catch (...) {
        // Important don't return an exception to the caller, it may crash
        logException("unexpected exception caught");
    }
}

/// log the exception message
public void logException(const char* const msg)
{
    // write message in a file... 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文