9

There are multiple questions (1,2,3,4 etc. etc.) called "Why isn't this exception caught". Sadly, none of these solutions work for me... So I am stuck with a truly uncatchable exception.

I have a piece of code (.NET 4.0) that checks a large textfile for digits and numbers. Whilst testing I got a runtime exception:

Exception

What you see here is a try-catch pattern with a catchblock for an ArgumentOutOfRangeException. But during runtime, the try block throws an ArgumentOutOfRangeException that is not being caught.

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

A catch block of a try statement is reachable if the try statement is reachable.

So in theory the above code should catch the exception.

Then I thought it might had something to do with the fact that this code is running in a task (during the processing of the textfile I also want to update the UI so I do it asynchronous). I searched around and then I found this answer by Jon Skeet. Basically suggesting I use Task.Wait in a try-catch block to catch any exceptions.

The problem I am facing now is that I can't really call Task.Wait because that would block the calling thread which is my UI thread! Then I figured that I could create an extra tasklayer to wait for that 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 this still gives the same result... Then I thought that it could be because of the fact I am not specific enough with my Exceptiontype. But the C# Language Specification states:

Some programming languages may support exceptions that are not representable as an object derived from System.Exception, although such exceptions could never be generated by C# code.

So unless you use some sketchy third party API you're always good when you use Exception. So I found myself with an suggested answer by Jon Skeet that didn't quite work for me. That's when I knew I should just stop trying...

So does anyone know what is going on? And how can I fix this? I know I could just check if i is equal or bigger than text.Length but understanding what's happening is more important than working code.

Community
  • 1
  • 1
Jordy
  • 1,816
  • 16
  • 29

2 Answers2

7

This is simply an artifact of the debugger.

In the Debug menu, there's an option called Exceptions... Click it, and make sure to uncheck the "Thrown" checkbox here:

exceptions

Many times, you'll want to see the error in context, even if it's inside a try/catch, which is what this setting is for. In this case, that is exactly what you ought to be doing, so that you can see compare i to the length of text and see where your problem is.

If you ran the code without the debugger (such as by double-clicking the executable or using the "Start without Debugging" option), you would "correctly" throw away the error without any alerts.

Bobson
  • 13,498
  • 5
  • 55
  • 80
  • Why doesnt my VS not list the user-handled column? Is it because I am using Express edition? – nawfal Aug 05 '13 at 13:44
  • That worked! Thank you! But why doesn't he do this for every Thrown exception? I wonder how this checkbox got checked because it isn't default in my other projects... – Jordy Aug 05 '13 at 13:48
  • 1
    @nawfal - See [this question](http://stackoverflow.com/questions/2631305/visual-studio-what-happened-to-the-break-when-an-exception-is-unhandled-optio). – Bobson Aug 05 '13 at 13:51
  • 1
    @Jordy - No idea. I've had it mysteriously check or uncheck itself before, and haven't been able to spot why. – Bobson Aug 05 '13 at 13:51
  • @Bobson, ah thanks. Still a bit mysterious but atleast I know it wasn't my fault ;) – Jordy Aug 05 '13 at 13:55
  • 1
    @nawfal, usally I tend to wait an half hour - hour to prevent giving an accepted answer when another could be better. But seeing as this worked and it is as simple as that, I could ignore it for once ;) – Jordy Aug 05 '13 at 14:04
  • You're welcome. Unfortunately, I'm overly familiar with this particular problem - my inherited codebase has a *lot* of `catch (Exception ex) {}` in there, so it makes debugging a pain whether it's checked (and catches unrelated errors that I can ignore) or not (and throws away the error I want to see). I'm slowly repairing it, but it's painful. – Bobson Aug 05 '13 at 16:45
0

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 had another exception, which is not called in your calling code.

There is only one exception I know which can't be caught. It's the StackOverflowException. See this question about it.

Community
  • 1
  • 1
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • I think that `ExecutingEngineException` can't be catched. – xanatos Aug 05 '13 at 13:48
  • The screenshot clearly depicts that an ArgumentOutOfRangeException ocurred. Why would you think it would be a StackOverflowException instead? The question you linked doesn't mention anything about this. – Lars Kristensen Aug 05 '13 at 13:50
  • "I believe that you had another exception", how can it be different from what the printscreen says? Or what exactly do you mean? – Jordy Aug 05 '13 at 13:50
  • I think that the exception you see on the screen is caught in the catch block bellow. I assume that something is not working with you application, that's why you debug it. But this problem is not because of the exception on your screen shot. – Stefan Steinegger Aug 05 '13 at 13:54
  • Hey, catching an exception doesn't mean that it doesn't get thrown anymore. You still see the exception get thrown, but it is caught later on. – Stefan Steinegger Aug 05 '13 at 13:58
  • @LarsKristensen: read the last sentence of the answer again. I didn't say that a stack overflow occurs somewhere in the code. – Stefan Steinegger Aug 05 '13 at 13:59
  • @StefanSteinegger, my mistake, I misread the last sentence. Still, I don't think the debugger would show one kind of exception, if the code actually threw another (There could be nested exceptions, but the screenshot shows the outermost) – Lars Kristensen Aug 05 '13 at 14:19
  • @Lars: I don't know what you are talking about. I never said that the exception on the screen is not there or something else. – Stefan Steinegger Aug 05 '13 at 14:21
  • @StefanSteinegger, The OP had a problem with an exception being thrown, but not caught. Your answer suggests that another kind of exception is being thrown that was not being caught. However, OP also tried catching "Exception" which wasn't being caught either, so it wasn't the type of Exception that was the problem. – Lars Kristensen Aug 05 '13 at 14:44
  • @Lars: Most probably it is not a different exception, it's a different place. E.g. after the code that is on the screen shot. – Stefan Steinegger Aug 06 '13 at 06:33
  • @StefanSteinegger, That wasn't the OP's problem, and he has already accepted an answer, which was about Debugger-settings - but I'd like to know why you would think that un-caught exceptions elsewhere could be related to this question? – Lars Kristensen Aug 07 '13 at 07:34
  • @Lars: because I didn't expect that the OP doesn't know about the basics about C# and Visual Studio. I expected that he has an actual problem with his application, and that therefor an exception is thrown somewhere, but not at the place he is looking at. – Stefan Steinegger Aug 07 '13 at 09:20
  • @StefanSteinegger, Alright. I kinda disagree on the debugger-settings of Visual Studio as being "basics", but if it truly is, then I would assume that to be the first thing to recognize in the OP's problem. But lets end the commenting here - I'm not trying to degrade your answer, I was just curious because I don't know that much in detail about exceptions, so I wanted to know if there were some special cases to take care of :) – Lars Kristensen Aug 07 '13 at 10:54