Note that there are some explanatory texts on larger screens.

plurals
  1. POMoby Dick of exceptions
    primarykey
    data
    text
    <p>There are multiple questions (<a href="https://stackoverflow.com/q/10866307/2294020">1</a>,<a href="https://stackoverflow.com/q/16548901/2294020">2</a>,<a href="https://stackoverflow.com/q/36014/2294020">3</a>,<a href="https://stackoverflow.com/q/12596242/2294020">4</a> 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.</p> <p>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:</p> <p><img src="https://i.stack.imgur.com/0AlfK.png" alt="Exception"></p> <p>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.</p> <p>I read the <a href="http://msdn.microsoft.com/en-us/library/ms228593(v=vs.80).aspx" rel="nofollow noreferrer">C# language specification</a> section about the try-catch structure, and it says:</p> <blockquote> <p>A catch block of a try statement is reachable if the try statement is reachable.</p> </blockquote> <p>So in theory the above code should catch the exception.</p> <p>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 <a href="https://stackoverflow.com/a/16548966/2294020">this</a> answer by Jon Skeet. Basically suggesting I use Task.Wait in a try-catch block to catch any exceptions.</p> <p>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:</p> <pre><code>//Code called from the UI System.Threading.Tasks.Task.Factory.StartNew(()=&gt; { //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!"); } }); </code></pre> <p>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:</p> <blockquote> <p>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. </p> </blockquote> <p>So unless you use some sketchy third party API you're always good when you use <code>Exception</code>. 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...</p> <p>So does anyone know what is going on? And how can I fix this? I know I could just check if <code>i</code> is equal or bigger than <code>text.Length</code> but understanding what's happening is more important than working code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload