Moby Dick Exceptions

There are several questions ( 1 , 2 , 3 , 4 , etc.) called "Why this exception did not hit." Unfortunately, none of these solutions work for me ... So I'm stuck with a really elusive exception.

I have a piece of code (.NET 4.0) that checks a large text file for numbers and digits. During testing, I got an exception at runtime:

Exception

Here you see a try-catch pattern with catchblock to throw an ArgumentOutOfRangeException. But at run time, the try block throws an ArgumentOutOfRangeException exception that does not get caught.

I read the C # language spec about the try-catch structure, and it says:

Locking a try statement can be achieved if a try statement is available.

So, in theory, the above code should catch an exception.

Then I thought that this could be due to the fact that this code works in the task (during the processing of the text file, I also want to update the interface so that I make it asynchronous). I searched and then found this answer by Jon Skeet. Basically, I suggest using Task.Wait in a try-catch block to catch any exceptions.

The problem I'm currently facing is that I cannot name Task.Wait, because it blocks the calling thread, which is my UI thread! Then I decided that I could create an additional tasklayer to wait for this task:

//Code called from the UI System.Threading.Tasks.Task.Factory.StartNew(()=> { //Create a new task and use this task to catch any exceptions System.Threading.Tasks.Task task = System.Threading.Tasks.Task.Factory.StartNew(MethodWithException); try { task.Wait(); } catch(Exception) { MessageBox.Show("Caught it!"); } }); 

But it still gives the same result ... Then I thought that it could be due to the fact that I'm not specific enough for my Exceptiontype. But the C # language specification says:

Some programming languages ​​may support exceptions that cannot be represented as objects originating from System.Exception, although such exceptions can never be thrown by C # code.

So, if you do not use some fragmentary third-party API, you will always be fine when using Exception . So I found myself with John Skeet's suggested answer, which didn’t quite work for me. This is when I knew that I just had to stop trying ...

So does anyone know what is going on? And how can I fix this? I know that I can simply check if i is equal to or greater than text.Length , but understanding what is happening is more important than working code.

+7
c # exception
source share
2 answers

This is just a debugger artifact.

In the Debug menu, select the Exceptions... option Exceptions... Click on it and make sure to uncheck the "Abandoned" box:

exceptions

In many cases, you need to see the error in context, even if it is inside try/catch , for which this parameter is intended. In this case, this is exactly what you should do so that you can compare i with the length of text and see where your problem is.

If you run the code without a debugger (for example, by double-clicking the executable file or using the "Start without debugging" option), you will "correctly" throw an error without any warnings.

+7
source share

I just wrote the following test:

  [TestMethod] public void ArgumentOutOfRangeExceptionTest() { string test = "abc"; int i = 0; try { while (true) { test.ElementAt(i); i++; } } catch (ArgumentOutOfRangeException) { } } 

It is working fine. I believe that you have another exception that is not being thrown in the calling code.

I know only one exception that cannot be caught. This is a StackOverflowException . See this question about this.

0
source share

All Articles