Re: try, catch, finally- whats the point of finally?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alan Paulk (apaulk_at_141.com)
Date: 05/04/04


Date: 4 May 2004 02:54:47 -0700


"Justin Rogers" <Justin@games4dotnet.com> wrote in message news:<emoTLxYMEHA.1348@TK2MSFTNGP10.phx.gbl>...
> Darnit, sorry, the Ctrl+Enter bug hit again.
>
> Take this code:
>
> try {
> // Throw your exception
> } finally {
> // Run your clean-up here
> }
>
> The exception will bubble up the call stack and get caught by
> another catch block if one exists, but your finally code will still
> get run. If you placed the code outside of the finally block, then
> execution would stop at the exception and be thrown up the stack
> to be caught. Let's take a resource allocation example.
>
> try {
> // My library accesses a stream
> // Some error happens and I need to throw an exception for the user.
> throw new Exception("Invalid data was in the stream");
> } finally {
> // Close the stream here
> }
>
>
> --
> Justin Rogers
> DigiTec Web Consultants, LLC.
> Blog: http://weblogs.asp.net/justin_rogers
>
> "Alan Paulk" <apaulk@141.com> wrote in message
> news:60a93123.0405031922.44bb2db2@posting.google.com...
> > It seems to me if you have a function like this
> >
> > public void blah()
> >
> > try
> > {
> > blah blah;
> > }
> >
> > catch
> > {
> > blah de blah;
> > }
> >
> > #code that executes whether there is an error or not#
> >
> > return;
> >
> > Then the code at the end will execute just as if it were in a finally
> > block. If that is the case then I don't know what the point of a
> > finally block is. Couls someone clear that up for me?
> >
> > -Alan

Thanks for the help!
-Alan



Relevant Pages

  • Re: stack questions
    ... It is said that on encountering an exception, the execution engine ... | "unwinds" the call stack looking for a suitable exception handler. ... | simply in unwinding during normal flow. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Try Finally...
    ... finally continues at the point where the exception is handled. ... the Windows mechanism takes a two-stage ... with the first stage only following the stack and the second ... execution finally continues after the handler that handles the exception. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Program execution in AS400
    ... A newbie from Mainframe into the world of AS400. ... faces a unmonitored exception. ... the execution of program is ended in ... the system has a very robust and well structured call stack. ...
    (comp.sys.ibm.as400.misc)
  • Re: Program execution in AS400
    ... faces a unmonitored exception. ... the execution of program is ended in ... the system has a very robust and well structured call stack. ... what to do when the *escape message is unmonitored for. ...
    (comp.sys.ibm.as400.misc)
  • Re: try, catch, finally- whats the point of finally?
    ... >> The exception will bubble up the call stack and get caught by ... >> execution would stop at the exception and be thrown up the stack ... an absolute godsend! ...
    (microsoft.public.dotnet.languages.csharp)