Lazarus

Programming => General => Topic started by: Robert W.B. on May 25, 2020, 11:15:38 pm

Title: Try Expection don't show?
Post by: Robert W.B. on May 25, 2020, 11:15:38 pm
Hi Friends.
I try to use "Try Exception" to notify my users that my file on server is unvailable at the moment but,
I have got an debugger error mesage instead!

How can i get my own message instead and not file not found message by Lazarus?

Try

UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil);

 except
     on E: Exception do begin
       ShowMessage('The Server is unavailable at the moment! Try later.');
 end;
 end;     


See the pix i attached with this question? IGet rid of this messagebox and replaced with my own message instead.

Thanks in advance
Bob
Title: Re: Try Expection don't show?
Post by: eljo on May 26, 2020, 12:06:57 am
Hi Friends.
I try to use "Try Exception" to notify my users that my file on server is unvailable at the moment but,
I have got an debugger error mesage instead!

How can i get my own message instead and not file not found message by Lazarus?

Try

UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil);

 except
     on E: Exception do begin
       ShowMessage('The Server is unavailable at the moment! Try later.');
 end;
 end;     


See the pix i attached with this question? IGet rid of this messagebox and replaced with my own message instead.

Thanks in advance
Bob
Have you tried to execute the program oustide the debugger? Does it have the same behavior?
Title: Re: Try Expection don't show?
Post by: Robert W.B. on May 26, 2020, 12:29:58 am
Hi eljo. Is the same messages in a different pc i tried!
It's like the debugger is in the exefile?
/Bob
Title: Re: Try Expection don't show?
Post by: eljo on May 26, 2020, 01:53:18 am
Hi eljo. Is the same messages in a different pc i tried!
It's like the debugger is in the exefile?
/Bob
The most possible cause is that you did not enclose the correct piece of code inside the try except block.
Title: Re: Try Expection don't show?
Post by: jamie on May 26, 2020, 01:53:47 am
maybe you need this instead..

Exception.Create('Your Message Striing');
Title: Re: Try Expection don't show?
Post by: winni on May 26, 2020, 02:01:45 am
Hi!

The Unit URLmon  is just a wrapper for a Windows library.
The error happens inside that library.

But UrlDownloadToFile is a function - you get an errorResult, that you dont use.

Code: Pascal  [Select][+][-]
  1. error : HResult:
  2. ...
  3. error := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil);
  4.  
Look in the unit URLMon in the fpc sources - there are all the error values defined.

And in the end:

Don't use Windows function: Trouble.

Winni
Title: Re: Try Expection don't show?
Post by: Remy Lebeau on May 26, 2020, 02:25:17 am
The Unit URLmon  is just a wrapper for a Windows library.
The error happens inside that library.

But UrlDownloadToFile is a function - you get an errorResult, that you dont use.

In other words, UrlDownloadToFile() does not raise an exception into user code on failure, which is why your try..except block doesn't catch anything.

It should also be noted that UrlDownloadToFile() is notoriously buggy, and its error codes are not very useful.  If you want to know WHY a download fails, you are better off using InternetReadFile() or WinHttpSendRequest()/WinHttpReceiveResponse() instead.
TinyPortal © 2005-2018