Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Quite some exceptions cannot be caught and which these are differs from .NET version to .NET version. And some exceptions can be caught and logged, but cannot be recovered from (memory exceptions). However, it is possible to debug them (these are called <a href="http://support.microsoft.com/kb/105675?wa=wsignin1.0" rel="noreferrer"><em>first chance exceptions</em></a>, the first chance is always for the debugger, the second chance is for the code, thanks to JeroenH for pointing this out). Here's a <a href="http://www.codeproject.com/KB/dotnet/UnCaught_Exceptions.aspx" rel="noreferrer">post at CodeProject that explains this</a> principle.</p> <p>What you should do is select some candidate exceptions in Visual Studio that you suspect might occur, and attach the debugger to the running instance.</p> <p>Having unmanaged resources as you have (serial port) means you can have unmanaged leaks (not using <code>IDisposable</code> + <code>using</code> properly) and unmanaged exceptions. These exceptions can only be caught with an empty <code>catch</code> (i.e., without specification of even <code>Exception</code>, which is <em>not</em> the parent of unmanaged exceptions) in a try/catch block.</p> <p>PS: some undefined behavior can occur when exceptions are raised in finally blocks or in finalizers/destructors. Also, not many exceptions propagate across thread boundaries and terminate all threads.</p> <h2>Edit</h2> <p>To make things a little clearer, there are a few exceptions that the CLR (and its specification) define as non-catchable. Basically, these are all exceptions that cross thread boundaries. These asynchronous exceptions, when occurring within a lock, will result in state corruption. Best known are <code>OutOfMemoryException</code>, <code>ThreadAbortException</code> and <code>StackOverflowException</code>. When the <code>OutOfMemoryException</code> or <code>StackOverflowException</code> occurs in synchronous code, it's unlikely that you can correct state and the CLR will terminate your application.</p> <p>In addition there's the <code>ExecutionEngineException</code> and <code>BadImageFormatException</code> which should not happen in verifiable code and should not be caught. Exceptions such as the <code>TypeLoadException</code> and <code>MissingMemberException</code> can sometimes be caught and sometimes not (if a linked assembly is missing, it'll be hard to catch these, and you shouldn't, but if you use reflection, you should catch these).</p> <p>In short: exceptions should be caught in the thread they happen in. You will not be able to catch exceptions if they happen in another thread, because they are not propagated (with the exception of the <code>ThreadAbortException</code>). Your application stays alive after an exception (at least, you think), so it is logical to assume that the exception doesn't happen in the thread where you're trying to catch it. Using the Debug > Exceptions window, you can select any exception and break on the code when they happen.</p> <h2>Note on Exception</h2> <p>An added note on managed and unmanaged exceptions. You cannot catch an unmanaged exception using <code>catch (Exception e)</code>, because the unmanaged exception does not inherit from <code>Exception</code>. Instead, use an empty catch, which will trap any unmanaged exception for you. Wrap this around your application and thread entrypoint methods and you should be able to catch the majority of catchable exceptions.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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