Catching Exceptions

Please discuss general Delphi programming topics here.

Catching Exceptions

Postby Kambiz » December 14th, 2007, 1:22 pm

I want to handle exceptions raised during form creation inside the dpr file, but try except block doesn't catch the exceptions.

Can anybody explain why?

Code: Select all
begin
  Application.Initialize;
  try
    Application.CreateForm(TDM, DM);
    Application.CreateForm(TMainForm, MainForm);
  except
    on E: Exception do
    begin
      TErrorDialog.Execute(E);
      Exit;
    end;
  end;
  Application.Run;
end.


By the way, I use Delphi 7 on XP SP2.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby john123 » December 18th, 2007, 12:23 pm

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 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 tryblocks 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.

TExceptionEvent is the type of the OnException event. It points to a method that handles exceptions in the application. The Sender parameter is the object that raised the exception, and E is the exception object.
Note:
You can also respond to this event using the TApplicationEvents component, which allows you to assign an event handler using the IDE.
john123
Active Member
Active Member
 
Posts: 15
Joined: November 7th, 2007, 10:43 am

Postby Kambiz » December 18th, 2007, 9:58 pm

Thanks for the info.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 8 guests

cron