C ++是否可以忽略函数中的异常? [英] Does C++ have a way to ignore an exception from a function?

查看:89
本文介绍了C ++是否可以忽略函数中的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个引发异常的函数,但在某些情况下,我希望它简单地忽略此异常.

I created a function that throws an exception, but under some circumstances I want it to simply ignore this exception.

我是这样写代码的,但是不太优雅:

I wrote my code like this, but it's not quite elegant:

try {
    myFunction();
} catch (...) {}

C ++是否有另一种编写方法?

Does C++ another way to write this?

推荐答案

不,没有.

在这种情况下,您可以遵循标准的做法,即两次使用 std :: nothrow_t ,一次也没有.使用后者包装第一个

you can follow what the standard does in this case which is to overload the function twice, once with std::nothrow_t and once without. use the later to wrap the first

std::error_code my_function(std::nothrow_t) noexcept;
void my_function(); //throws

这篇关于C ++是否可以忽略函数中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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