1

I've recently run into this problem, and it doesn't make sense.

the following snippet is real:

 try
  {
    File.Create(targetFile);
    //File.WriteAllText(targetFile, $"test {DateTime.Now.ToString()}");
  }
  catch (UnauthorizedAccessException uaex)
  {
  }

I have checked it step by step, as soon as i get with the debugger to the "File.Create()" method, the exception rises, and it doesn't enter the catch block, also, if i remove the try-catch, it doesn't bubble up to the calling of the method which contains this.

Anyone got any idea why the try-catch and the bubbling doesn't work?

ps. The location where I am trying to create the file is write protected on purpose, this is just a way to check if it is.

Akos
  • 210
  • 4
  • 14
  • Where in the program do you have this code? – Lasse V. Karlsen May 30 '16 at 07:43
  • Its a quite complex project, this method is called from a background thread when the application is starting – Akos May 30 '16 at 07:48
  • I'm just giving you an alternative, to check if directory is write only protected, you can check solution on that link: http://stackoverflow.com/questions/7511592/check-if-folder-is-read-only-in-c-net – Excalibrus May 30 '16 at 07:49
  • @Excalibrus i don't think that will work in all cases for all users with different rights to the directory. – Akos May 30 '16 at 07:50
  • @LasseV.Karlsen I've made a test application where i try to "confuse" things with BackgroundWorkers that run tasks on main thread that create background threads to run the same try-catch that i posted. And I can't seem to be able to recreate the problem with multi threading nor with 1 thread. – Akos May 30 '16 at 08:08

1 Answers1

0

I've made a mistake.

The exception is actually being caught, if you put anything in the catch block, it does execute.

To be fair the debugger confused me, by showing the exception pop-up right at the calling of the method, but that was solved by restarting the IDE

Akos
  • 210
  • 4
  • 14