Topic: try, catch, throw statements
Author: Raja R Harinath <harinath@cs.umn.edu>
Date: 1998/02/02 Raw View
"Mark Wilden" <Mark@mWilden.com> writes:
> Steve Clamage wrote in message <6aqhlc$gd2@engnews1.Eng.Sun.COM>...
> >
> >Because braces are required [in a catch statement], the questions
> don't arise.
> >I consider that a large advantage.
>
> Does this apply only to catch-statements, or would you also like to
> see braces mandatory in if-statements as well?
`try/catch', and `if/else' are not exactly analogous.
An `if' can be followed by zero or one `else' clause.
A `try' can be followed by one or more `catch' clauses.
The dangling `catch' is much more difficult than the dangling `else',
and the easiest solution would be to require braces to explicitly
disambiguate them. The dangling `else', however, has a bearable enough
solution without explicit disambiguation.
- Hari
--
Raja R Harinath ------------------------------ harinath@cs.umn.edu
"When all else fails, read the instructions." -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing." -- Roy L Ash
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: NOSPAMsl@psycode.com (Gili)
Date: 1998/01/27 Raw View
Greetings,
I was wondering if the ANSI/ISO standards specify that try, catch,
throw statements must be blocks.. I mean, while I know the following
is valid:
"try
{
<something>
cout << "covered by try command";
}
"
Is this following code invalid?
"try
<something>
cout << "not covered by try command";
"
Also, if I place a try command around my entire function body and
then handle all caught exceptions at the end of the function, does
this make my algorithm less efficient than if I would have placed
try{}, catch{} commands around the specific exception-throwing
functions? Thank you for your help,
Gili
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/01/29 Raw View
Gili wrote:
>
> Greetings,
>
> I was wondering if the ANSI/ISO standards specify that try, catch,
> throw statements must be blocks..
Yes, they must be. I wish it weren't so, though.
> Also, if I place a try command around my entire function body and
> then handle all caught exceptions at the end of the function, does
> this make my algorithm less efficient than if I would have placed
> try{}, catch{} commands around the specific exception-throwing
> functions? Thank you for your help,
Generally, I think it's a wash. The compilers I use generally create a single
exception context on the stack on entry to any function that has anything to do
with exceptions, and then mark progress through the function by incrementing
some thread-local variable, to indicate what needs to be done (or undone) when
an exception occurs. It does this regardless of whether you've got one big try
block or lots of little ones. There might be a slight advantage to the one big
block, but it's very slight. Make the decision based on how easy the code
writes itself.
--
Ciao,
Paul
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: clamage@Eng.sun.com (Steve Clamage)
Date: 1998/01/29 Raw View
In article 599CD78F@ix.netcom.com, "Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
>Gili wrote:
>>
>> Greetings,
>>
>> I was wondering if the ANSI/ISO standards specify that try, catch,
>> throw statements must be blocks..
>
>Yes, they must be. I wish it weren't so, though.
Why? Because you must type two extra characters (the pair of braces)
around a single statement? That seems like a small disadvantage.
The advantages are that the scoping is explicit and you don't have a
dangling-catch problem, analagous to the dangling-else problem inherited
from C.
I'd rather type 100 extra characters than spend an hour hunting for
a misplaced or incorrectly-indented try or catch.
Suppose you could write code like this:
try
try
int j = foo();
catch(int)
f();
catch(char*) // which try does this apply to?
g();
cout << j; // is j in scope here?
Because braces are required, the questions don't arise.
I consider that a large advantage. You might not agree.
---
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Mark Wilden" <Mark@mWilden.com>
Date: 1998/01/29 Raw View
Steve Clamage wrote in message <6aqhlc$gd2@engnews1.Eng.Sun.COM>...
>
>Because braces are required [in a catch statement], the questions
don't arise.
>I consider that a large advantage.
Does this apply only to catch-statements, or would you also like to
see braces mandatory in if-statements as well?
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1998/01/30 Raw View
"Mark Wilden" <Mark@mWilden.com> writes:
> Steve Clamage wrote in message <6aqhlc$gd2@engnews1.Eng.Sun.COM>...
> >
> >Because braces are required [in a catch statement], the questions
> don't arise.
> >I consider that a large advantage.
>
> Does this apply only to catch-statements, or would you also like to
> see braces mandatory in if-statements as well?
I don't think anyone seriously proposed that for the C++ standard, and
I don't think that anyone will seriously propose it for C++ 0X, but
there's no denying that "dangling else" bugs would have been less
common if C had made the braces mandatory in if statements.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]