Recent

Author Topic: Nested try...except...[finally] vs try...catch...finally in other oopL  (Read 16009 times)

Goodman H__

  • Full Member
  • ***
  • Posts: 130
Greetings,

**Disclaimer:I am not intended to compare apple by apple and to say which one is better,just want to learn more about fp's memory management mechanism.

In fp if I have to do some cleanups whatever an eception was caught or not,a possible solution is to nest try except and try ...finally.

Code: [Select]
try
   try
   // to-dos
   except
   ...
finally
   cleanups();
In some modern oopl like C# and Java,this is implemented by
Code: [Select]

try
{
   //to-dos
}
catch(...)
{
   ...
}
finally
{
   cleanups();
}

Compare the two implementation,I am wondering is there any performance penalty in FP's implementation when we are talking about stack unwinding,CPU circle,and anything else been affected?

Thanks for your help in advance.
 
« Last Edit: August 27, 2010, 02:31:07 am by Goodman H__ »
fpc:2.6.1 Lazarus:1.1 SVN39277
OS:win 7

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9663
  • Debugger - SynEdit - and more
    • wiki
Re: Nested try...except...[finally] vs try...catch...finally in other oopL
« Reply #1 on: August 27, 2010, 03:23:44 am »
I don't know for sure, so I may be wrong.

But afaik, each "try..." creates an exception frame. So if you nest two of them, it's double the work.

No idea if the c construct does it with one?

But for what you want to do, you don't need to nest two try blocks

Code: [Select]
 try
    Allocate();
    Something();
  except
    DoHandleException;
  end;
  // you handled the exception, so you will always be here
  // unless you gor an exception while handling the exception
  DeAllocate();

if you think handling the exception may fail itself
Code: [Select]
 except
    try
      DoHandleException;
     except
       // do nothing on 2nd exception, so never fail here
     end;
  end;
This code has 2 exception frames, but the 2nd will only be created, if there is an exception, so normally doesn't matter.


If you want to reraise the exception
Code: [Select]
 LastExcept := nil;
  try
    Allocate();
    Something();
  except
    On e: exception do begin
       DoHandleException;
       LastExcept := e;    
     end;
  end;
  DeAllocate();
  if e <> nil then raise e;


Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2582
Re: Nested try...except...[finally] vs try...catch...finally in other oopL
« Reply #2 on: August 29, 2010, 05:11:24 pm »
For the last example you can also reverse finally and except block.

Anyway I don't know about efficiency of having 2 nested blocks or 2 separate blocks. I aso don't know if C internally creates 2 for the construct given by the OP.

Also personally I keep the allocation outside the try..finally block, since if the allocation fails, there is nothing deallocate, which may result in another exception.

Code: Pascal  [Select][+][-]
  1. Allocate();
  2. try
  3.   try
  4.     Something();
  5.   finally
  6.     DeAllocate();
  7.   end;
  8. except
  9.   DoHandleException;
  10. end;
  11.  

Anyway, nowadays I use less exceptionhandlers than I used before. Only code which gets external input (user/file) is checked. All other should work. If not there is a logic flaw in my code and makes no sense to continue the app.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018