Re: What to put in the try-block
On 7 Feb, 10:19, Ian Collins <ian-n...@hotmail.com> wrote:
> Erik Wikström wrote:
> > I just thought of a question that I have never seen discussed before
> > (perhaps it is trivial?) and while I think I know the answer I realise
> > that others might have other oppinions, which is why I ask it here.
>
> > How much code should one put in the try-block, i.e. if you have code
> > such as this:
>
> > foo;
> > bar;
> > baz;
> > try {
> > somecode;
> > }
> > catch (exception& e) {
> > // ...
> > }
>
> > and you have to posibility to also put foo, bar, and baz in the try-
> > block, should you do so?
>
> > I think no, since by limiting the stuff in the try-block I more
> > clearly indicate what might throw (or which depends on something not
> > throwing) than I do if I put as much as possible in it. Are there any
> > other oppinions or motivations?
>
> Well that all depends on whether you can recover form an exception in
> foo and then continue to execute bar and baz. If not, you may as well
> put the lot in the try block.
In the example I assume that foo, bar, and baz can not throw, only
somecode can. So the question could be reformulated as: is there any
value in including more code than that which can throw, or that which
depends on successful execution of the code that can throw, in the try
block?
--
Erik Wikström
|