Forum > General

Nested try...except...[finally] vs try...catch...finally in other oopL

(1/1)

Goodman H__:
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: ---try
   try
   // to-dos
   except
   ...
finally
   cleanups();

--- End code ---
In some modern oopl like C# and Java,this is implemented by

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

--- End code ---

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.
 

Martin_fr:
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: ---  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();

--- End code ---

if you think handling the exception may fail itself

--- Code: ---  except
    try
      DoHandleException;
     except
       // do nothing on 2nd exception, so never fail here
     end;
  end;

--- End code ---
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: ---  LastExcept := nil;
  try
    Allocate();
    Something();
  except
    On e: exception do begin
       DoHandleException;
       LastExcept := e;    
     end;
  end;
  DeAllocate();
  if e <> nil then raise e;

--- End code ---

Marc:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Allocate();try  try    Something();  finally    DeAllocate();  end;except  DoHandleException;end; 
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.

Navigation

[0] Message Index

Go to full version