When is a function try block useful?

后端 未结 6 1509
忘了有多久
忘了有多久 2020-11-30 06:31

I\'m wondering when programmers use function try blocks. When is it useful?

void f(int i)
try
{
   if ( i  < 0 ) 
      throw \"less than zero\";
   std::         


        
6条回答
  •  悲哀的现实
    2020-11-30 06:55

    You use it in constructors to catch errors from initializers. Usually, you don't catch those errors, so this is a quite exceptional use.

    Otherwise, it is useless: unless I'm proven wrong,

    void f() try { ... } catch (...) { ... }
    

    is strictly equivalent to

    void f() { try { ... } catch (...) { ... } }
    

提交回复
热议问题