Author Topic: Access Denied  (Read 1723 times)

user5

  • Sr. Member
  • ****
  • Posts: 419
Access Denied
« on: June 23, 2019, 01:33:21 pm »
    Is the code below an acceptable way to use a try statement with OpenDialog?
    I'm trying to provide protection against an Access Denied error. Thanks.

   
Code: Pascal  [Select][+][-]
  1. try
  2.  try
  3.   begin
  4.    if opendialog1.Execute = true then
  5.     begin
  6.      filename := opendialog1.FileName;
  7.     end
  8.   end;
  9.   except
  10.   On E: Exception do
  11.    begin
  12.     sound(350);
  13.     newerror := true;
  14.    end;
  15.  end;
  16. finally;
  17. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 19632
  • Glad to be alive.
Re: Access Denied
« Reply #1 on: June 23, 2019, 01:41:55 pm »
empty finally sections are never acceptable. (Same goes for empty except statements)
I also miss a specialized exception EAccessDenied and a re-raise if it is something else......
Bad programming, sorry.
« Last Edit: June 23, 2019, 01:45:31 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

user5

  • Sr. Member
  • ****
  • Posts: 419
Re: Access Denied
« Reply #2 on: June 23, 2019, 02:28:13 pm »
    Thank you for your response. Could you give me a brief example of something that is better?

ASerge

  • Hero Member
  • *****
  • Posts: 2514
Re: Access Denied
« Reply #3 on: June 24, 2019, 03:11:54 pm »
    Thank you for your response. Could you give me a brief example of something that is better?
In Windows, "OpenDialog" handles access denied errors before closing, so the code:
Code: Pascal  [Select][+][-]
  1. OpenDialog1.Execute;
Until you tell us more, it's better than your code.

Thaddy

  • Hero Member
  • *****
  • Posts: 19632
  • Glad to be alive.
Re: Access Denied
« Reply #4 on: June 24, 2019, 03:17:25 pm »
Indeed It would help a great deal.
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018