Recent

Author Topic: try .. finally  (Read 637 times)

Paolo

  • Hero Member
  • *****
  • Posts: 639
try .. finally
« on: October 11, 2025, 11:44:05 am »
Hello ,
simple question, and at the moment I think the answer is NO...

Is there a convenient way to know if the code inside Finally..End block is excuted because there is an active Exception and not as part of normal code flow ?

Code: Pascal  [Select][+][-]
  1. try
  2. ..
  3. finally
  4. ..           <--- I want to know if I am here as normal code flow or by a raised exception
  5. end;
  6.  

Regards.

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: try .. finally
« Reply #1 on: October 11, 2025, 11:51:33 am »
Clear a variable at the beginning of your code for example before the try and at the end of the code block before the finally set the variable if the code gets there you can test the variable within the finally
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 639
Re: try .. finally
« Reply #2 on: October 11, 2025, 11:57:35 am »
that is what I do. You confirm there si not "dedicated" way to detect such situation.

thanks.

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: try .. finally
« Reply #3 on: October 11, 2025, 03:47:23 pm »
of course there is a dedicated way to do this.

Use an Exception instead of both.

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 18306
  • Here stood a man who saw the Elbe and jumped it.
Re: try .. finally
« Reply #4 on: October 11, 2025, 04:13:06 pm »
Jamie,

You might also have explained that try/finally and try/except are different beasts.
They have comparable syntax but serve a different purpose.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: try .. finally
« Reply #5 on: October 11, 2025, 04:34:11 pm »
Jamie,

You might also have explained that try/finally and try/except are different beasts.
They have comparable syntax but serve a different purpose.
Excuse ME!

Code: Pascal  [Select][+][-]
  1.   Try
  2.     Try
  3.      Beep;
  4.   Except
  5.     Beep;
  6.   end;
  7.   finally
  8.    Beep;
  9.   end;                  
  10.  

Something of that order.

Jamie
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 639
Re: try .. finally
« Reply #6 on: October 11, 2025, 04:45:14 pm »
this does not answer the question.

The code in the finally block is executed regardless a exception is raised or not, so it is still not konw if an exception is raised or not when code in finally block is executed.

better stay with the flag approach.

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: try .. finally
« Reply #7 on: October 11, 2025, 05:28:09 pm »
You can insert a BREAK in the exception block there and it will not execute the Finally.

At least it should not.

Ops that does not work oh well.

 maybe abort? it compiles,
jamie


« Last Edit: October 11, 2025, 05:34:42 pm by jamie »
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 6184
  • Compiler Developer
Re: try .. finally
« Reply #8 on: October 11, 2025, 06:25:30 pm »
You can insert a BREAK in the exception block there and it will not execute the Finally.

At least it should not.

A break does not skip finally-blocks.

maybe abort? it compiles,

Abort is simply a raise EAbort.

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: try .. finally
« Reply #9 on: October 11, 2025, 11:56:24 pm »
You are correct about the "break", it won't even compile.
 :o
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018