RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomApplicationEvents.OnException Event

Occurs when an unhandled exception occurs in the application.

Pascal
property OnException: TExceptionEvent;
C++
__property TExceptionEvent OnException;

Use OnException to change the default behavior that occurs when an exception is not handled by application code. The OnException event handler is called automatically in the application's HandleException method.  

OnException only handles exceptions that occur during message processing. Exceptions that occur before or after the execution of the application's Run method do not generate OnException events. 

If an exception passes through the try blocks in the application code, the application automatically calls the HandleException method. Unless the exception object is EAbort, HandleException calls the OnException handler, if one exists. Otherwise, it calls ShowException to display a message box indicating an error occurred.

Note: Call the CancelDispatch method from an OnException event handler to prevent the application from forwarding the event to any other application events objects.
 

C++ Examples: 

 

/*
This example requires a TApplicationEvents and a TListBox
in the form.  Select the TApplicationEvents, double click
on the OnException event and add the following code
to the handler.  Trigger this event by calling the
Exception Create constructor.
*/
void __fastcall TAppEventForm::ApplicationEventsException(TObject *Sender,
      Exception *E)
{
    lbOther->Items->Add("Event OnException: " + E->Message);
}

void __fastcall TAppEventForm::MenuExceptionClick(TObject *Sender)
{
    char *sExceptionRaised = "This is an exception";
    throw Exception(sExceptionRaised);
}

 

Delphi Examples: 

{
This example requires a TApplicationEvents and a TListBox
in the form.  Select the TApplicationEvents, double click
on the OnException event and add the following code
to the handler.  Trigger this event by calling the
Exception Create constructor.
}
procedure TMainForm.ApplicationEventsException(Sender: TObject;
  E: Exception);
begin
  lbOther.Items.Add('Event OnException: ' + E.Message);
end;

procedure TMainForm.MenuExceptionClick(Sender: TObject);
resourcestring
  sExceptionRaised = 'This is an exception';

begin
  raise Exception.Create(sExceptionRaised);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!