2

How to avoid an error from displaying the little Windows error box?
Try and Except dont work because the error isnt showned by Delphi but from Program or I think from Windows.
enter image description here
try
Size:=TFileStream.Create(BitFile,fmOpenRead);
except on E: EFCreateError
do EC.Add('Error: ' + IntToStr(GetLastError));
end;

Little Helper
  • 2,419
  • 9
  • 37
  • 67
  • 5
    A better version of this question would indicate that you had done some work to figure out where the error message was coming from, and defined what "external event" means exactly. – Warren P May 29 '11 at 15:49
  • True. No way you can tell what is happening here is there is not error, not code and not even sure where the message comes from. – GolezTrol May 29 '11 at 16:13
  • 2
    @Robrok: I am glad to see that you didn't give up on developing, after all. In this case, however, you are unlikely to get any good answers unless you explain what "the little windows error box" is. Perhaps you could give us a screenshot of the dialog, so we at least know what kind of error box you are talking about... – Andreas Rejbrand May 29 '11 at 16:17
  • Similar question: http://stackoverflow.com/questions/3316692/how-can-i-supress-delphi-datasnap-error-message-dialogs – mjn May 29 '11 at 17:01
  • +1 think of background applications which must run without human interaction. Third party or system libraries popping up 'Click Ok to continue' - error boxes (and blocking the app) need special treatment. – mjn May 29 '11 at 17:13
  • How to delete my self from stackoverflow because any that i post question will be closed. – Little Helper May 29 '11 at 17:40
  • 2
    I have tried using executable outside debugger. And still I get the ****** error but @GolezTrol found an answer to this. I have added TApplicationEvents to my form and OnEeception added Memo1.Lines.Add('Error: ' + IntToStr(GetLastError); – Little Helper May 29 '11 at 18:30
  • @Robrok: what is the value of `E.ClassName` in your `OnException` handler method? – Jeroen Wiert Pluimers May 29 '11 at 20:52
  • 1
    I removed my down-vote when you made your question better, RobRok, don't give up on SO. You just have to give people something they can answer. – Warren P May 30 '11 at 05:04
  • True. The question is now reopened. – GolezTrol May 30 '11 at 06:30

3 Answers3

7

Is the error shown in your application? Otherwise put, is it an unhandled exception? Or is it a box displayed by Windows or by an external application?

You say 'event', but event handlers can contain try..except blocks too.

If it is an exception, and you don't know where it's coming from, you can use the TApplicationEvents class to attach the Application.OnException event. It will fire on all unhandled exceptions. There you can catch it, or rather, set a breakpoint and use the stack trace to see where the exception is coming from.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
3

An error box doesn't imply an exception has been raised. An error box can be explicitly shown in code.

So, it seems your question is "How can I prevent 3rd party code from working As Designed?". Beside decompiling the binaries, I'm afraid I can't suggest much, especially if you don't have the source.

If you have the source code and know the routine that needs to be replaced, you could write your own replacement and "hijack" the routine at runtime. This is the method used by, for example, the fastcode project to replace delphi's routine without recompiling the VCL. You can see the implementation in their project.

http://fastcode.sourceforge.net/

Unit: FastcodePatch.pas

Ken Bourassa
  • 6,363
  • 1
  • 19
  • 28
-4

Here it is

 private
    { Private declarations }
  public
   procedure MyExceptionHandler(Sender : TObject; E : Exception ); //define exception handler
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyExceptionHandler(Sender:TObject;E:Exception);
begin
    //Do nothing
end;

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

procedure TForm1.Button1Click(Sender: TObject);
begin
  //Generate an exception
  asm
  mov eax,8272
  mov [eax],$2FFFFF
  end 
end;
opc0de
  • 11,557
  • 14
  • 94
  • 187
  • any reason you are downvoting my answer ?? because i can't see none – opc0de May 29 '11 at 18:32
  • 3
    @opc0de: No explanation of what the code is doing (or isn't doing) or how it answers the question...a big ball of code isn't helpful. (I know i'd never just paste in some code without understanding it.) Aside from that...inline asm? I'm going to bet there are far better ways to generate an exception in Delphi...like, oh i dunno, `raise`ing one. I didn't downvote, cause i don't know Delphi enough to judge this code...but if i did, those would likely be the reasons. – cHao May 29 '11 at 18:49
  • 1
    2 obvious reasons to downvote - *magic* pointer 8272 and *magic* DWORD 2FFFFFh – Premature Optimization May 29 '11 at 21:13
  • 1
    @opc0de, i doubt you can explain magic numbers behind your "method" to trigger an exception. Also, besides not being hysteric when critized, you should not post code snippets which invites sudden WTFs from the readers. – Premature Optimization May 29 '11 at 22:03
  • All the more reason the numbers shouldn't have been so odd. Trying to write a 0 to address 0, i would think, should cause the same issue as writing 0x2fffff to address 8272. But even so, why be all arcane about causing an error, when raising an exception in Delphi would serve the same purpose? The only reason i can come up with, is you feel like showing off at the expense of your answer's clarity. – cHao May 30 '11 at 06:30
  • cHaO , if you wrote in your life more then 3-4 lines of code you know how to generate an exception as long as the answer is NOT WRONG i don't see a reason of downvote but i REALLY don't care! – opc0de May 30 '11 at 07:18
  • 3
    if you'd just changed the asm into a raise statement and added an explanatory paragraph you would have got lots of upvotes – David Heffernan May 30 '11 at 07:33