|
 |
Warp wrote:
> By the way, I honestly wonder what's the "proper" way of doing that
> in Java, given that Java has exceptions and no scope-bound lifetime of
> objects.
try {
handle = open(...);
} finally {
if (handle != null) handle.close();
}
Even uglier because you obviously have to declare the handle outside the
block, check to see if the assignment worked, etc.
> Do you always have to follow file handle creation with a 'try'
> block to guard against exceptions? Or is there some other trick to make
> such code exception-safe?
try/finally is what you use for making code exception safe.
> (I understand that in C# this is handled with a 'using' block, which
> automatically and immediately disposes of objects when the block is
> exited. Is that the proper way of making file handles exception-safe
> there?)
Yep. You're basically supposed to put anything with a "Dispose" method
inside a using block. (Well, unless you're assigning it to something more
global, etc.) More elegant than Java, but certainly not elegant, especially
since it's not always obvious what classes have a Dispose method without
reading the documentation, so it's easy to miss something if you're working
with a new library.
--
Darren New, San Diego CA, USA (PST)
Serving Suggestion:
"Don't serve this any more. It's awful."
Post a reply to this message
|
 |