Recent

Author Topic: Sender:TObject in Custom Exception Handler is Nil  (Read 855 times)

oldpicker

  • Newbie
  • Posts: 1
Sender:TObject in Custom Exception Handler is Nil
« 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!

« Last Edit: May 07, 2021, 11:27:22 am by oldpicker »

Bart

  • Hero Member
  • *****
  • Posts: 4281
    • Bart en Mariska's Webstek
Re: Sender:TObject in Custom Exception Handler is Nil
« Reply #1 on: May 07, 2021, 01:01:03 pm »
How does that even compile?
TObject does not have a methoed SetFocus AFAIK.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 4563
Re: Sender:TObject in Custom Exception Handler is Nil
« Reply #2 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.
The only true wisdom is knowing you know nothing

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 930
    • Lebeau Software
Re: Sender:TObject in Custom Exception Handler is Nil
« Reply #3 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?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018