Re: Try Finally...

From: Duncan McNiven (duncan_at_mcniven.net)
Date: 10/25/04


Date: Mon, 25 Oct 2004 14:45:10 +0100

On Mon, 25 Oct 2004 08:23:21 -0400, L D Blake <not@any.adr> wrote:

>
> Program test;
> var
> x,y : longint;
>
> procedure doit(z:longint);
> begin
> try
> y := 100;
> x := y div z;
> finally
> writeln('in the finally block now');
> end;
> end;
>
> begin
> doit(0);
> writeln('if you''re right you will see this');
> end;
>
>
>The program will terminate in the finally block

No it won't. It will continue at the next outer try .. finally or try
... except construct. Since you haven't provided one, execution goes to
the application's default exception handler, and terminates there.

>and you will never see the
>final writeln "if you're right you will see this');

Correct, because you have not provided a try ... except construct to
handle the exception, or a try ... finally construct to ensure the final
writeln is executed despite the exception.

>There are two conditions under which finally is entered...
>
> 1) Normally... the code is executed and the program continues.

Yes

> 2) Exception... the code is executed and the program terminates.

Only because you have provided no code to handle the exception. It
doesn't have to be like that; this behaviour is your choice. If you want
it to do something different, you have to write some code.

Test this:

program Test;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  x,y : longint;

procedure doit(z:longint);
begin
   try
      y := 100;
      x := y div z;
   finally
      writeln('in the finally block now');
   end;
end;

begin
   try
      doit(0);
   except
      on EDivByZero do begin
         writeln('How can you see this if FINALLY terminated to app?');
         readln;
      end;
   end;
end.

-- 
Duncan


Relevant Pages

  • Re: Possible huge clue into unmanged/managed problem
    ... If you happen to abort a thread asynchronously, that means an exception can ... when the exception is thrown during the execution of the finalize clause ... > terminate the whole application if I only want to terminate one thread? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Possible huge clue into unmanged/managed problem
    ... If you happen to abort a thread asynchronously, that means an exception can ... when the exception is thrown during the execution of the finalize clause ... > terminate the whole application if I only want to terminate one thread? ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Stopping a program
    ... > I'd like to stop a program (i.e. terminate its execution) without ... > raising an exception if some condition is met, ...
    (comp.lang.python)
  • Re: tasks and protected types
    ... > the same exception is also raised by the execution of the corresponding ... > Which means that D1.Start raises an exception, D2.Start is never called, ... > (D1 will terminate due to the unhandled exception) ... Msg:= Str; ...
    (comp.lang.ada)
  • Re: Runtime Syntax Checker
    ... If any Tcl command sets a return code other than zero, ... script to abort, but any following scripts will execute. ... In C, you can ignore errors, execution ... The caller must then handle the exception. ...
    (comp.lang.tcl)