Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In delphi 7, is `try ... except raise; end;` meaningful at all?

In some Delphi 7 code I am maintaining, I've noticed a lot of instances of the following:

with ADOQuery1 do begin
  // .. fill out sql.text, etc
  try
    execSQL;
  except
    raise;
  end;
end;

It seems to me that these try blocks could be removed, since they do nothing. However, I am wary of possible subtle side-effects..

Can anyone think of any instances in which these blocks could actually do anything that wouldn't happen without them there?

like image 401
Blorgbeard Avatar asked Apr 30 '09 02:04

Blorgbeard


2 Answers

In this context, the raise operation has no effect and should be removed becuase its simply re-raising the exception that the exception block just caught. raise is typically used to transfer control to the end of the block when no appropriate error handling is available. In the following we handle the custom exception, but any other exception should be handled elsewhere.

try
  someOperation;
except
  on e: ECustomException do
    SomeCustomHandelr;
  else
     begin
       // the raise is only useful to rethrow the exception to an encompasing 
       // handler.  In this case after I have called my logger code. as Rob
       // mentioned this can be omitted if you arent handling anything because
       // the compiler will simply jump you to the next block if there is no
       // else.
       LogUnexpectedException('some operation failed',e);
       raise;
     end;
end;

Be careful that there is similar looking form without the "raise" that DOES have the side effect of eating/hiding any exceptions. practicies by very unscrupulous developers who have hopefully moved on to positions with the competition.

with ADOQuery1 do begin  
  // .. fill out sql.text, etc  
  try    
    execSQL; 
  except
    // no handler so this just eats any "errors"    
  end;
like image 106
MikeJ Avatar answered Nov 07 '22 00:11

MikeJ


Removing the except code in above code snippet will make no difference. You can (and I believe you should since it is reducing the readability) remove it.

like image 36
Hemant Avatar answered Nov 06 '22 22:11

Hemant