Link to home
Start Free TrialLog in
Avatar of adamy
adamyFlag for Canada

asked on

How to stop MaskEdit exception?


I used TMaskEdit, EditMask is shortTime, when I input something wrong such as 1_:00, It will raise an Exception that said 'Invalid input value' and exit the program. Can I stop the exception? If I input wrong,can it just let me input again?
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

unit Unit1_Q_21081083;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask;

type
  EWinInet = class(Exception);

  TForm1 = class(TForm)
    MaskEdit1: TMaskEdit;
    Memo_Error: TMemo;
    procedure FormCreate(Sender: TObject);
  private   { Private declarations }
    procedure On_App_Exception(Sender: TObject; E: Exception);
  public    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  UnitMsgShow;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnException := On_App_Exception;
end;

procedure TForm1.On_App_Exception(Sender: TObject; E: Exception);
begin
  Memo_Error.Clear;
  Memo_Error.Lines.Add(E.message);
end;

end.

//........

object Form1: TForm1
  Left = 224
  Top = 128
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Form1'
  ClientHeight = 154
  ClientWidth = 322
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object MaskEdit1: TMaskEdit
    Left = 8
    Top = 8
    Width = 120
    Height = 21
    EditMask = '!90:00;1;_'
    MaxLength = 5
    TabOrder = 0
    Text = '  :  '
  end
  object Memo_Error: TMemo
    Left = 8
    Top = 40
    Width = 288
    Height = 104
    ReadOnly = True
    TabOrder = 1
  end
end
Avatar of DarkCore_
DarkCore_

In no case an exception should close the program itself automatically.

Most sure something else is wrong, maybe it produces somewhere in your code a stack overflow ( a function which calls itself once and twice without end ). Maybe you've set up an event like onChange or OnExit?

Edu
Avatar of adamy

ASKER

Why I can't catch the error? There are no errors in my program, but it cannot catch the error.

1. Declare the custom exception handler
{ Public declarations }
procedure MyExceptionHandler(Sender : TObject; E : Exception );

2. Define the exception handler in the "implementation" section:

procedure TForm1.MyExceptionHandler(Sender : TObject; E : Exception );
var
     wRetVal : Word;
begin
     wRetVal := MessageDlg('ERROR: ' + E.Message, mtError, mbAbortRetryIgnore, 0 );
     case wRetVal of
         mrAbort: begin
             { handle "Abort" here... }
         end;
         mrRetry: begin
             { handle "Retry" here... }
         end;
         mrIgnore: begin
             { handle "Ignore" here... }
         end;
         else
         begin
             { handle "other" action here...}
         end;
     end;
end;

3. assign the newly created exception handler

procedure TForm1.FormCreate(Sender: TObject);
begin
     { begin new code }
     Application.OnException:=MyExceptionHandler;
     { end new code }
end;
example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21081083.zip
ASKER CERTIFIED SOLUTION
Avatar of gandalf_the_white
gandalf_the_white
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the problem with this solution is that you dont get any exceptions in your code anymore

that can be a problem as coding errors may also be hidden from you by delphi.

there are different solutions for this problem.
i like this one the most:

set a breakpoint in the codeline before the exception will happen.
open the breakpoint properties (by rightclicking on it)
there you have the option to show further options (i have german delphi so
i don't know how it is called in english).
down there you have an options that is called ignore further exceptions. check this
and you won't get an exception for the rest of the program. if you uncheck the "hold"
(maybe its called "stop")  option you won't even notice that there is a breakpoint.
after the lines you can set another breakpoint that also doesnt stop and in this one you
check the "treat exceptions" checkbox and in the rest of the program you'll get your
exceptions as you deserve them ;-)

regards