In Delphi Pascal there is a command called TRY. It has 2 flavours:
This one optionally has a way to return info about the exception.

 Code:
try
    ; do something that shouldn't break, but did once and you have NFI why
except
    ; send an email with info about the failure
end


and
 Code:
; turn something on that needs to be turned off
try
    ; do something that if it fails would abort the sctipt
finally
    ; turn that something off
end


In this case, if the something failed, then the finally...end block would execute, no matter what. You need to be careful not to put anything in that block that depends on the something working of course, but it means you can be sure that your clean up routine always runs.

When I read the OP, it felt like a request for a try...except loop. And I believe it's a good idea. You can't always write code to handle every problem, especially when you are calling out to other code (COM etc). If something fails in that external call, there's no guarantee that your calling process will return politely.

So while you wouldn't go wrapping every single routine in a try loop, some routines that perform tasks whose failure may/should be silent to the user may need to be mentioned to an administrator would be cleanly handled (barring massive kix32.exe failure of course).

Delphi has an exception object that contains info about the type of exception and the actual problem. For example:
 Code:
try
    $myinteger = StringToInteger('1 2')
except on E:Exception do
    ShowMessage('Exception: "' + E.Message + '" of class "' + E.ClassName + '" occured trying to convert')
end


returns a popup message

Exception: "'1 2' is not a valid integer value" of class "EConvertError" occured trying to convert

You can take this to the Nth degree of course:
 Code:
try
    $mystring = 'Convert "' + $userinput + '"'
    $myinteger = StringToInteger($userinput)
    $mystring = 'Call external DLL with ' + $myinteger
    $=ExternalCall($myinteger)
except on E:Exception do
    SendEmail('Exception: ' + E.Message + ' while trying to ' + $mystring)
end;


This method lets you see what the last action attemped was.

my AUD 0.02

cj