Re:Exception Safety, Garbage Collection etc. (a.k.a. Why Doe...
by mquander on Friday May 06, 2011 @01:20PM
(#2521648)
I take it that you're imagining a case like this:
That's true, the scoping protects you from making that mistake. However, the more common case is this:
There are two things I can do to fix the latter case. I can either declare the variable outside the block, adding a silly-looking extra line of code, or I can move the print() within the block, which can be problematic -- it means that if print() were something time-consuming, I would be holding onto the resource during print() for no reason. I think this is a crappy thing to force onto the programmer.
[ Parent
| on HN ]
Re:Exception Safety, Garbage Collection etc. (a.k.a. Why Doe...
by bmm6o on Friday May 06, 2011 @02:59PM
(#2522065)
Sure. Another alternative is to pull the try/catch/finally into a separate function that just returns the value. But that's not always convenient, YMMV, etc. Either way, I'd consider it just an occasional annoyance (occasional because frequently print() is quick, or the resource is not expensive or contended).