Recent

Author Topic: Try Expection don't show?  (Read 1123 times)

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
Try Expection don't show?
« 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
Rob

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Try Expection don't show?
« Reply #1 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?

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
Re: Try Expection don't show?
« Reply #2 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
Rob

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Try Expection don't show?
« Reply #3 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.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Try Expection don't show?
« Reply #4 on: May 26, 2020, 01:53:47 am »
maybe you need this instead..

Exception.Create('Your Message Striing');
The only true wisdom is knowing you know nothing

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Try Expection don't show?
« Reply #5 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

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Try Expection don't show?
« Reply #6 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.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018