相关疑难解决方法(0)

函数try块何时有用?

我想知道程序员何时使用函数try块.什么时候有用?

void f(int i)
try
{
   if ( i  < 0 ) 
      throw "less than zero";
   std::cout << "greater than zero" << std::endl;
}
catch(const char* e)
{
    std::cout << e << std::endl;
}

int main() {
        f(1);
        f(-1);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:(在ideone处)

greater than zero
less than zero
Run Code Online (Sandbox Code Playgroud)

编辑:因为有些人可能认为函数定义的语法不正确(因为语法看起来不熟悉),我要说它不是不正确的.它叫做function-try-block.参见C++标准中的§8.4/ 1 [dcl.fct.def].

c++ function function-try-block

36
推荐指数
6
解决办法
9144
查看次数

std :: uncaught_exception如何工作?

介绍

有关于使用和理性的帖子文章std::uncaught_exception.

功能提供的功能归结为

std :: uncaught_exception检测堆栈展开当前是否正在进行中.

Q

在搜索其定义时,我看到的是对DLL的调用

_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL uncaught_exception();
Run Code Online (Sandbox Code Playgroud)

如何实现程序中的这种内省水平?这是仅仅通过c ++或编译器/汇编魔法实现的东西必须发挥作用吗?

c++ c++11

1
推荐指数
1
解决办法
217
查看次数

标签 统计

c++ ×2

c++11 ×1

function ×1

function-try-block ×1