Recent

Author Topic: How to catch the EDBEditError exception  (Read 5929 times)

fedkad

  • Full Member
  • ***
  • Posts: 176
How to catch the EDBEditError exception
« on: September 03, 2016, 10:12:33 pm »
I have a TMaskEdit box in my form. When the user enters invalid data and leaves the box, an EDBEditError exception is raised. What is the correct way to catch this exception and handle as I wish?
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How to catch the EDBEditError exception
« Reply #1 on: September 03, 2016, 10:52:53 pm »
You can use OnEditingDone and validate the text in a try..except block and correct wrong input.
This then you must do for all TMaskEdit's.

Alternatively you can use an Application.OnException handler and handle EDBEditError there.
Sender will be the TMaskEdit that raised the exception.

Bart

fedkad

  • Full Member
  • ***
  • Posts: 176
Re: How to catch the EDBEditError exception
« Reply #2 on: September 04, 2016, 03:02:18 pm »
Thanks Bart for your response.

I finally decided to move away from TMaskEdit. Considering the validation code that I need to put in OnEditingDone, I can go just with the standard TEdit box.
 ::)
Lazarus 2.2.6 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How to catch the EDBEditError exception
« Reply #3 on: September 04, 2016, 03:55:31 pm »
In an Application.OnException handler, you could simply do:

Code: [Select]
procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
  if E is EDBEditError then
  begin
    ShowMessage('Unacceptable input: please try again');
    (Sender as TMaskEdit).SetFocus;
  end
  else
    Application.ShowException(E);
end;

Similar can be done in OnEditingDone or OnExit:

Code: [Select]
...
    try
      (Sender as TMaskEdit).ValidateEdit;
    except
      ShowMessage('Unacceptable input: please try again');
      (Sender as TMaskEdit).SetFocus;
    end;
...

In both cases you let TMaskEdit do the validating for you.

Bart
« Last Edit: September 04, 2016, 04:07:30 pm by Bart »

Thaddy

  • Hero Member
  • *****
  • Posts: 14206
  • Probably until I exterminate Putin.
Re: How to catch the EDBEditError exception
« Reply #4 on: September 04, 2016, 05:37:36 pm »
Thanks Bart for your response.

I finally decided to move away from TMaskEdit. Considering the validation code that I need to put in OnEditingDone, I can go just with the standard TEdit box.
 ::)

What in the name of whomever you may adore is an "amount of validation"? :)
Plz show me why you think that is the case.
If it's more than three to four lines of code you are definitely doing it wrong. I guess any validation will take me two lines.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14206
  • Probably until I exterminate Putin.
Re: How to catch the EDBEditError exception
« Reply #5 on: September 04, 2016, 05:43:39 pm »
In an Application.OnException handler, you could simply do:

Code: [Select]
procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
  if E is EDBEditError then
  begin
    ShowMessage('Unacceptable input: please try again');
    (Sender as TMaskEdit).SetFocus;
  end
  else
    Application.ShowException(E);
end;

Similar can be done in OnEditingDone or OnExit:

Code: [Select]
...
    try
      (Sender as TMaskEdit).ValidateEdit;
    except
      ShowMessage('Unacceptable input: please try again');
      (Sender as TMaskEdit).SetFocus;
    end;
...

In both cases you let TMaskEdit do the validating for you.

Bart
@Bart
I would not promote the use of exceptions except in exceptional circumstances.
You showed how NOT to use exceptions, Bart. Even if that code works.
Unacceptible input is a valid code path and should be handled gracefully in code, not by exceptions.
Exceptions are to be used on unforeseen code paths that are beyond the control or knowledge of the programmer..
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14206
  • Probably until I exterminate Putin.
Re: How to catch the EDBEditError exception
« Reply #6 on: September 04, 2016, 05:50:41 pm »
I have a TMaskEdit box in my form. When the user enters invalid data and leaves the box, an EDBEditError exception is raised. What is the correct way to catch this exception and handle as I wish?

The correct way is not to let the exception occur in the first place. In most cases a run over the entered string with in or charinset of set of char is enough to validate input against invalid input.
That's two lines of code. Can be written in one line.
You do not want exceptions that you more or less can predict. Exceptions are very costly and meant for other purposes.
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How to catch the EDBEditError exception
« Reply #7 on: September 04, 2016, 05:53:53 pm »
I would not promote the use of exceptions except in exceptional circumstances.
You showed how NOT to use exceptions, Bart. Even if that code works.
Unacceptible input is a valid code path and should be handled gracefully in code, not by exceptions.
Exceptions are to be used on unforeseen code paths that are beyond the control or knowledge of the programmer..

You miss the point completely.
TMaskEdit raises an exception by design (because Dephi does this), when you leave the control and the Text does not match EditMask.
So, whenever you put a TMaskEdit on a form and set EditMask (and EditMaks does NOT only contain 'c' with or withou any mask literal), then you MUST handle that exception, otherwise you get the dreadfull message box that invites you to close the application or fear data-loss.

Feel free to not handle the exception that WILL occur (users always seem to be able to enter invalid data), but your user's will NOT thank you for that.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How to catch the EDBEditError exception
« Reply #8 on: September 04, 2016, 06:02:14 pm »
The correct way is not to let the exception occur in the first place.
In most cases a run over the entered string with in or charinset of set of char is enough to validate input against invalid input.

This defeats the purpose of using a TMaskEdit in the first place.
(But, I must admit that for almost any situation there are better controls available.)
Also, TMaskEdit will handle drag+drop/cut/paste for you in such a way that it will obey the EditMask.
If you want to implement such a behaviour as well then "a few lines of code" simply won't do.

Exceptions are very costly and meant for other purposes.

Costly in what sense?
AFAIK I do not have to pay money for exceptions  O:-)
Probably they cost much more time and overhead than a simple boolean check, but remember this is about user-input, so even if an exception would take 100 ms, it really would not matter at all (unless Superman was behind the keyboard).

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14206
  • Probably until I exterminate Putin.
Re: How to catch the EDBEditError exception
« Reply #9 on: September 04, 2016, 06:28:04 pm »
It is not about practice but about the theory behind exceptions. If it is really designed in such a way than that is bad design.
It violates the principle that exceptions should be exceptional
See: http://www.stroustrup.com/except89.pdf (chap 13) amongst other, more theoretical literature.
« Last Edit: September 04, 2016, 06:33:18 pm by Thaddy »
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How to catch the EDBEditError exception
« Reply #10 on: September 04, 2016, 06:49:35 pm »
It is not about practice but about the theory behind exceptions. If it is really designed in such a way than that is bad design.

Please thank the Greek (as in: Delphi) for that.
If I would have written it from scratch, it would not raise an exception if validation failed, but probably fire an event.

Note that onDelphi you cannot even intercept the exception being raised (it is raised before OnExit is called).
I implemented it slightly different in Lazarus.

Bart

tonyp

  • New Member
  • *
  • Posts: 25
Re: How to catch the EDBEditError exception
« Reply #11 on: August 06, 2020, 08:30:34 pm »
Quote
Note that onDelphi you cannot even intercept the exception being raised (it is raised before OnExit is called).
I implemented it slightly different in Lazarus.

A few years later and I also had the same problem, so I tried your exception catching suggestion, but it did not work for me (Lazarus 2.0.8/FPC 3.04).

I tried placing this code:

Code: Pascal  [Select][+][-]
  1.   try
  2.     (Sender as TMaskEdit).ValidateEdit;
  3.   except
  4.     ShowMessage('Unacceptable input: please try again');
  5.     (Sender as TMaskEdit).SetFocus;
  6.   end;
  7.  

inside either 'OnEditingDone' or 'OnExit' and I still get the Exception.

If there is no reasonable place to put this 'try' block before the exception fires, then the whole control is practically useless, unfortunately.

 

TinyPortal © 2005-2018