Positioning of catch(...)

T

Taras_96

Hi everyone,

I recently came across a quiz question. Does the standard say anything
about catch(...) being the first catch statement?

try
{
}
catch(...) // <- 'hides' subsequent catching
{
}
catch(A)
{
}
catch(B)
{
}

I've seen numerous examples where catch(...) appears first (eg:
Bjorne), but code doesn't compile using MS C++, so does this mean that
the standard allows it, but some compilers don't?

Taras
 
K

Kai-Uwe Bux

Taras_96 said:
Hi everyone,

I recently came across a quiz question. Does the standard say anything
about catch(...) being the first catch statement?

Yes, catch statements are tried in order [15.3/5] and if present, a
catch(...) shall go last.

try
{
}
catch(...) // <- 'hides' subsequent catching
{
}
catch(A)
{
}
catch(B)
{
}

I've seen numerous examples where catch(...) appears first (eg:
Bjorne), but code doesn't compile using MS C++, so does this mean that
the standard allows it, but some compilers don't?

The standard says [15.3/6]:

A ... in a handler?s exception-declaration functions similarly to ... in a
function parameter declaration; it specifies a match for any exception. If
present, a ... handler shall be the last handler for its try block.

That says that a program where catch(...) is not the last handler in a block
is ill-formed ("shall").


Best

Kai-Uwe Bux
 
P

Pranav

Taras_96 said:
Hi everyone,
I recently came across a quiz question. Does the standard say anything
about catch(...) being the first catch statement?

Yes, catch statements are tried in order [15.3/5] and if present, a
catch(...) shall go last.


try
{
}
catch(...) // <- 'hides' subsequent catching
{
}
catch(A)
{
}
catch(B)
{
}
I've seen numerous examples where catch(...) appears first (eg:
Bjorne), but code doesn't compile using MS C++, so does this mean that
the standard allows it, but some compilers don't?

The standard says [15.3/6]:

  A ... in a handler?s exception-declaration functions similarly to ... in a
  function parameter declaration; it specifies a match for any exception. If
  present, a ... handler shall be the last handler for its try block.

That says that a program where catch(...) is not the last handler in a block
is ill-formed ("shall").

Best

Kai-Uwe Bux

From Wheer can I get C++ standard or it is just Bjarne Strostrup CPP
book..,
 
J

Juha Nieminen

Kai-Uwe Bux said:
That says that a program where catch(...) is not the last handler in a block
is ill-formed ("shall").

What does "ill-formed" mean in practice? Does it have something to do
with "undefined behavior"?
 
E

Erik Wikström

What does "ill-formed" mean in practice? Does it have something to do
with "undefined behavior"?

I believe that ill-formed code is not required to compile, while
undefined behaviour refers to the run-time behaviour (which means that
the code did compile).
 
J

James Kanze

What does "ill-formed" mean in practice? Does it have
something to do with "undefined behavior"?
[/QUOTE]
I believe that ill-formed code is not required to compile,
while undefined behaviour refers to the run-time behaviour
(which means that the code did compile).

Sort of. Ill-formed means that the compiler must issue a
diagnostic. Having done so, it is free to compile the code if
it wishes. (Good compilers generally don't.) Undefined
behavior means just that---whatever the compiler does with the
code is correct as far as the standard is concerned. It may
refuse to compile it (with or without an error message); it may
compile it do something that works; or it may compile it to do
anything else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top