Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catch without try compiling successfully

The following code compiles successfully with g++ version 5.1.0. Notice the catch without a corresponding try in the member function foo::bar(). I was wondering if this syntax was legal and if so what effect it had?

struct foo
{
  void bar()
  {
  }
  catch (...)
  {
  }
};

int main ()
{
  foo f;
  f.bar();
  return 0;
}

Originally seen in Catch block in constructor without try

like image 337
Richard Critten Avatar asked May 03 '26 04:05

Richard Critten


1 Answers

The example you give,

struct foo
{
  void bar()
  {
  }
  catch (...)
  {
  }
};

… is not valid standard C++.

It might be a g++ language extension.

The catches in the question you linked to look like function catch blocks, except that also that requires a try keyword.

like image 120
Cheers and hth. - Alf Avatar answered May 04 '26 16:05

Cheers and hth. - Alf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!