delphi - Shall I free/destroy exceptions after exception handling? -
i'm debugging delphi program.
except on e: terrortapedrive if e.errorcode = 1104 if assigned(indexdoneevent) indexdoneevent; // other handling... // other handling... end;
i catch excetion e , need. now, when debug program counter reach line below end;
, if hover e.errorcode
cursor, can still see value. expect out of scope and, eventually, destroyed.
so, question is: shall free/destroy exceptions after exception handling?
the runtime takes ownership of exceptions after raised. not need free them.
the exception destroyed @ end of block in handled, demonstrated program:
{$apptype console} uses sysutils; type myexception = class(exception) public destructor destroy; override; end; destructor myexception.destroy; begin writeln('myexception.destroy'); inherited; end; procedure main; begin try raise myexception.create('boo'); except on e: exception writeln(e.classname, ': ', e.message); end; writeln('after try/except block'); end; begin main; readln; end.
which outputs:
myexception: boo myexception.destroy after try/except block
whilst debugger may still show information exception after has been freed, behaviour undefined. compiler understands exception has left scope, if debugger ignorant of fact.
if want exception's lifetime extend beyond except
block handles need call acquireexceptionobject
. once that, becomes responsibility free exception lifetime acquired.
Comments
Post a Comment