0

I have some code in my application that throws an error every so often (System.AccessViolationException) - So I wrapped it in a try/catch block and set a debug point and logging method in the catch element. I've found that since I did this the error has stopped happening - the debug point is never hit and nothing is logged. As soon as I remove the try from around the code I get the error again. What could be causing this?

The code is pretty trivial:

 try
        {
            var f1 = new ResizeNearestNeighbor(lfu.Width, lfu.Height);
            var f2 = new Crop(ViewRectangle);
            lfu = f2.Apply(lfu);
            lfu = f1.Apply(lfu);
        }
        catch (Exception ex)
        {
            MainForm.LogExceptionToFile(ex);//never hit
        }
Sean
  • 2,033
  • 2
  • 23
  • 28
  • 4
    No, it can't. It will prevent the error from propagating up the call stack if it is caught and not thrown again. Just sounds like another intermittent bug, annoying aren't they. – Adam Houldsworth Jul 08 '13 at 10:52
  • Check if you have declared f1, f2 elsewhere in the code. – bansi Jul 08 '13 at 10:54
  • If you know its an AVException then why are you not catching this instead? – Sayse Jul 08 '13 at 10:55
  • Because I'm just trying to figure out what's wrong with it. Also the method successfully completing isn't critical to the operation of the application. – Sean Jul 08 '13 at 11:00

2 Answers2

1

There could be a couple of options:

  • or MainForm.LogExceptionToFile(ex); does not work as it expected

  • or, if we are 100% sure in that method, probably injecting try/catch block introduces in the code flow that microscopic delay (due the more IL code to execute/control), which is necessary to not get AccessViolationException on that machine. Which means, that is absolutely possible that you will get that exception on some other machine.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • MainForm.LogExceptionToFile(ex) definitely works (it's a static) i'm leaning towards your microscopic delay theory although it's pretty strange that it completely stops the error - i'd have thought that would just make it less likely... – Sean Jul 08 '13 at 11:03
  • @Sean: here is something about micro elay, imo, that on yuor machine "resolves" access problem (which is usually timing issue), but it can be, as you say. Which means, that, as you said, it becomes more rare on your machine and most probabbly still present on others – Tigran Jul 08 '13 at 11:06
0

A AccessViolation Exception can't be skipped or catched.

Every time this exception happens it would be thrown!