Lazarus

Programming => General => Topic started by: oldpicker on May 07, 2021, 06:46:17 am

Title: Sender:TObject in Custom Exception Handler is Nil
Post by: oldpicker on May 07, 2021, 06:46:17 am
I have written a custom exception handler which displays a customized error message when OnException.
What I want to do here is moving the text cursor back to the TDBEdit that caused this exception, after the error message is displayed.

I have tried the following code:

Code: Pascal  [Select][+][-]
  1. procedure Form1.FormCreate(Sender: TObject);
  2. begin
  3.    Application.OnException := @CustomExceptionHandler;
  4. end;
  5.  
  6. procedure Form1.CustomExceptionHandler(Sender: TObject, E: Exception);
  7. begin
  8.    ShowMessage(‘Invalid Input:+ E.Message);
  9.    Sender.SetFocus;
  10. end;
  11.  

which gives SIGSEGV error at Sender.SetFocus because the Sender in CustomExceptionHandler is nil.

What should I do if I want the cursor back to the field that caused the exception?
Thanks for your help in advance!

Title: Re: Sender:TObject in Custom Exception Handler is Nil
Post by: Bart on May 07, 2021, 01:01:03 pm
How does that even compile?
TObject does not have a methoed SetFocus AFAIK.

Bart
Title: Re: Sender:TObject in Custom Exception Handler is Nil
Post by: jamie on May 07, 2021, 11:36:42 pm
Code: Pascal  [Select][+][-]
  1.  If Sender is TWinControl then TWinControl(Sender).SetFocus;
  2.  

I am not really sure that will work for inside an exception handler.
Title: Re: Sender:TObject in Custom Exception Handler is Nil
Post by: Remy Lebeau on May 07, 2021, 11:47:04 pm
What I want to do here is moving the text cursor back to the TDBEdit that caused this exception, after the error message is displayed.

There is no way to know which control raised the exception.  But if the TDBEdit was already focused when the exception is raised, I would expect it to regain the focus when the error dialog is closed.

I have tried the following code:

The Sender of the OnException event is not the control that raised the exception.  It is usually the object that caught the exception and called TApplication.HandleException() to trigger the OnException event.  HandleException() has a Sender parameter that is passed to OnException.  That is *usually* the TForm.

So, I find it a little odd that the Sender is nil instead.  That means whichever object is calling HandleException() is not identifying itself.  What does the call stack look like when the exception is raised?
TinyPortal © 2005-2018