如何从捕获的异常打印信息? [英] How to print message from caught exception?

查看:1044
本文介绍了如何从捕获的异常打印信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题。真的很感激,如果C ++程序员可以指导我。我想写的C#代码如下C ++中的dll。 ?能否请您指导

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#代码翻译:

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 ++中类似的东西,但我不能尝试{}赶上(...)的消息部分。

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:

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;
}

这篇关于如何从捕获的异常打印信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆