Avoid "program stopped working" in C#/.NET

23,893

Solution 1

The JIT debugger popup occurs when there's an unhandled exception. That is, an exception tunnels all the way up the stack to the root of any thread in the runtime.

To avoid this, you can handle the AppDomain.CurrentDomain.UnhandledException event and just call Environment.Exit(1) to exit gracefully.

This will handle all exceptions on all threads within your AppDomain. Unless you're doing anything special, your app probably only has one AppDomain, so putting this in your public static void Main method should suffice:

AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
    Console.Error.WriteLine("Unhandled exception: " + args.ExceptionObject);
    Environment.Exit(1);
};

You should probably use the NAnt logger to write out the error in this case too (can't recall the API for this offhand though.)

You can also disable JIT debugging on the machine. I would only recommend this in certain circumstances such as for a dedicated build server.

Solution 2

Under Windows Vista you can disable this dialog for your programms.

Disable the "Problem Reports and Solutions feature". You find it under Control Panel-->Problem Reports and Solutions-->Change Settings-->Advanced Settings-->Turn off for my programs, problem reporting

Solution 3

Just catch the exception and log/ignore it.

Solution 4

The popup appears due to an unhandled exception. To avoid that make sure your main method captures all exceptions and turn them into some other useful piece of info you can pick up. Just ignoring the exception is not recommended.

Btw remember that exceptions are per thread, so if your application spawns threads or uses thread pool threads, you need a handler for these too.

Share:
23,893
Ole Lynge
Author by

Ole Lynge

My job is in software development. I have used these technologies and tools professionally -> spare time: Programming languages: Fortran, C# -> Python, Ruby User interface: ASP.NET WebForms/MVC, Silverlight, Javascript, HTML -> Ruby on rails Database: MSSQL -> SQLite, MySQL Source control: Subversion -> Git, Mercurial Build tools: MSBuild, NAnt, CruiseControl.NET Search engine: Fast ESP -> Lucene.NET Test tools: NUnit, Moq, Watin, Selenium, Spec Flow Dependency injection: Unity Caching: Velocity/AppFabric ORM: LinqToSql, Entity framework -> Subsonic Links: Blog Website Projects: History Based Estimation iCrawl: A simple crawler / link checker Custom code analysis rulesKata: Simple Membership in ASP.NET MVC

Updated on September 28, 2021

Comments

  • Ole Lynge
    Ole Lynge over 2 years

    I have a console application written in C#/.NET that I want to run from a script (nant). If an exception occurs in the console application, I would like nant to continue, but in Windows Vista there is a popup that searches for solutions and asks for debug etc.

    I would like to avoid the popup with "program stopped working" when an exception happens in the console application. How can I control this from C#/.NET?

    (A similar question addresses the issue for the C language, but I would like a solution for C#/.NET.)

    (To clarify: I would like the exception to be passed to nant, but without the popup.)