Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The catch clause in your snippet will never catch anything in the shipping version of your app. It <em>does</em> work when you run it with a debugger attached.</p> <p>What you are missing is the way Application.ThreadException behaves. That event fires whenever any unhandled exception is detected. This feature however is <strong>not</strong> enabled when you debug your code. No exception handler is installed to raise the event. This was done so you have a decent way to debug unhandled exceptions. Your code changes that behavior, now there <em>is</em> a try block active, your catch handler gets the exception.</p> <p>To get the code to behave the same way, you'll need to change the unhandled exception handling strategy. Like this:</p> <pre><code> [STAThread] static void Main() { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); Application.Run(new Form1()); } catch (Exception ex) { // etc.. } } </code></pre> <p>Now your catch clause will always catch the exception. As long as it is raised on the main thread, it won't catch exceptions raised in worker threads. Consider this code instead for unified handling:</p> <pre><code> [STAThread] static void Main() { AppDomain.CurrentDomain.UnhandledException += AllUnhandledExceptions; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); Application.Run(new Form1()); } private static void AllUnhandledExceptions(object sender, UnhandledExceptionEventArgs e) { var ex = (Exception)e.ExceptionObject; // Display or log ex.ToString() //... Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(ex)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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