Root > How to... > ...handle an exception? > ...emulate an exception?

...emulate an exception?

Previous pageReturn to chapter overviewNext page   

If you want to "handle" an exception which was not raised yet, e.g.:

 

E := Exception.Create('Message');
Application.ShowException(E);
FreeAndNil(E);

 

You should do it like this:

 

uses 
  EExceptionManager; // for ExceptionManager
 
// ...
 
ExceptionManager.StandardEurekaError('Message');

 

This will create pseudo-exception and trigger EurekaLog (to show dialog, save bug report, send, etc.).

 

Optionally, you may customize EurekaLog for such exceptions (e.g. disable sending, etc.):

 

uses 
  EClasses,          // for TEurekaModuleOptions
  EModules,          // for CurrentEurekaLogOptions
  EExceptionManager, // for ExceptionManager
  ETypes;            // for mtErrorMsgCaption
 
// ...
var

  Options: TEurekaModuleOptions;

begin
  Options := TEurekaModuleOptions.Create('');
  try
    Options.Assign(CurrentEurekaLogOptions);
 
    // This is just an example. Alter options as you like
    Options.CustomizedTexts[mtErrorMsgCaption] := 'Notification';
 
    ExceptionManager.StandardEurekaError('Message', Options);
  finally
    FreeAndNil(Options);
  end;

end;

 

 


 

However, if you don't want a pseudo-exception class, and need information about real exception object - then you have to specify some additional details, for example:

 

uses
  {$IFDEF EUREKALOG} 
  ECallStack,        // for GetCurrentCallStack and TEurekaBaseStackList
  EClasses,          // for TEurekaModuleOptions
  EExceptionManager, // for ExceptionManager
  ETypes,            // for mtErrorMsgCaption
  {$ENDIF}
  //  ...
 
//  ...
 
var
  E: Exception;
  {$IFDEF EUREKALOG}
  EI: TEurekaExceptionInfo;

  Stack: TEurekaBaseStackList;
  {$ENDIF}
begin
  E := Exception.Create('Message');
 
  {$IFDEF EUREKALOG}
  EI := ExceptionManager.Info(Pointer(E), nil);
  if Assigned(EI) then
  begin

    // Required, emulates "raising" of the exception

    Stack := GetCurrentCallStack;

    try 
      EI.CallStack := Stack;

    finally

      FreeAndNil(Stack); 

    end
    
    // Optional, just an example
    EI.Options.CustomizedTexts[mtErrorMsgCaption] := 'Notification';
  end;
  {$ENDIF}
 
  Application.ShowException(E);
  FreeAndNil(E);
end;

 

Some information related to exception's raising will be missed (such as exception's address, CPU, and assembly info), so you may want to disable these in options.

 

If you don't mind debugger's notifications - then you may use a shorter alternative:

 

try
  raise Exception.Create('Message');
except
  ApplicationHandleException(nil);
end;

 

 

See also:




Send feedback... Build date: 2024-12-19
Last edited: 2023-06-28
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/how_to_emulate_exception.php