April 01, 2004 Re: what is try-catch-finally? | ||||
---|---|---|---|---|
| ||||
On Thu, 01 Apr 2004 06:07:58 GMT (01/Apr/04 04:07:58 PM) , Karl Bochert <kbochert@copper.net> wrote: > On Wed, 31 Mar 2004 21:48:20 -0800, Andy Friesen <andy@ikagames.com> wrote: >> Karl Bochert wrote: >> >> >> >> try { <some statement(s) } >> >> catch (<errorclass>) { <do something about it> } >> >> finally { <always run> }; >> >> >> > >> > How does that differ from: >> > >> > try { <some statement(s) } >> > catch (<errorclass>) { <do something about it> } >> > <always run>}; >> >> Even if an exception is thrown, and isn't caught in this scope, the >> finally block will execute while the stack is being unwound. >> >> ie >> >> try { >> throw new Exception("This won't be caught here."); >> } catch (IOError error) { >> we can't catch Exception() here, only IOError >> } finally { >> // clean up the file, whether or not an error occurred >> myFile.close(); >> } >> >> -- andy > Makes sense -- sort of interleaved exceptions > Not for me, I think How come, Karl? I can see that I might need to close a file regardless of what type of error happened, and that I might also want to do some special stuff for specific types of errors. That's what the 'finally' phrase allows for. -- Derek |
Copyright © 1999-2021 by the D Language Foundation