C++怎么排列catch子句

这篇文章主要介绍“C++怎么排列catch子句”,在日常操作中,相信很多人在C++怎么排列catch子句问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么排列catch子句”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

E.31:正确排列catch子句

Reason(原因)

catch-clauses are evaluated in the order they appear and one clause can hide another.

catch子句按照它们表示的次序行,一个子句出发之后,其他子句不再执行。

Example(示例)

void f()
{
   // ...
   try {
           // ...
   }
   catch (Base& b) { /* ... */ }
   catch (Derived& d) { /* ... */ }
   catch (...) { /* ... */ }
   catch (std::exception& e) { /* ... */ }
}

If Derivedis derived from Base the Derived-handler will never be invoked. The "catch everything" handler ensured that the std::exception-handler will never be invoked.

如果Deriveds是Base的派生类,捕捉派生类的处理永远不会执行。捕捉所有异常的处理会导致捕捉std::exception的处理程序永远不会执行。

Enforcement(实施建议)

Flag all "hiding handlers".

标记所有被隐藏的异常处理程序。

到此,关于“C++怎么排列catch子句”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!