Console apps and AppDomain.CurrentDomain.UnhandledException

This one has me stumped. I’d swear this behaved differently prior to .NET 1.1 service pack 1 (and/or XP SP2), but I can’t prove it. As reported by a CodeProject reader, you’ll get the standard .NET crash dialog in a console app, even if you’ve registered an unhandled exception handler for your AppDomain. What gives? Why doesn’t AppDomain.CurrentDomain.UnhandledException capture exceptions on the main thread of a .NET console application?

But don’t take my word for it – try it yourself. Use this sample (source code zip file) from John Robbins, co-founder of Wintellect. Or, paste the code from this MSDN article into a new console app and run it:

Sub Main()
Dim cd As AppDomain = AppDomain.CurrentDomain
AddHandler cd.UnhandledException, AddressOf MyHandler
Try
Throw New Exception("1")
Catch e As Exception
Console.WriteLine("Catch clause caught : " + e.Message)
End Try
Throw New Exception("2")
' Expected output:
'   Catch clause caught : 1
'   MyHandler caught : 2
End Sub
Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
Console.WriteLine("MyHandler caught : " + e.Message)
End Sub

At first I was concerned that installing VS.NET had somehow forced me into some kind of bizarre first-chance exception mode exclusive to console applications, but not so. The compiled .exe behaves in the same way on every machine I tried it on: I get the standard .NET crash dialog, then after I dismiss that, I get the unhandled exception handler I wanted in the first place. That’s... not exactly the order I had in mind.

There’s a way to disable the .NET JIT debugging dialog, as described by Scott Hanselman. But that’s an extreme “solution” – it disables the crash dialog for all .NET apps. It’s also treating the symptoms rather than the disease: why can’t we catch unhandled exceptions in console apps any more? I’d swear this worked the last time I looked at it. And the MSDN sample code certainly implies that it’s possible – but good luck getting that sample to print the expected output.

So what am I missing here?

Related posts

Computers are Lousy Random Number Generators

The .NET framework provides two random number generators. The first is System.Random [http://msdn2.microsoft.com/en-US/library/system.random.aspx]. But is it really random? > Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a

By Jeff Atwood ·
Comments

Make Mine XCOPY

Steve “what the heck does furrygoat mean” Makofsky crystallized a lot of my thoughts in his recent rant on software installers. One of the biggest advantages of using the .NET framework is the way it enables XCopy deployments for the first time.* Installing a program by copying it to a

By Jeff Atwood ·
Comments

Creating Even More Exceptional Exceptions

In response to my previous post decrying the lack of a master list of Exception classes for .NET, a helpful reader pointed out a clever little utility buried in the .NET SDK: Program FilesMicrosoft Visual Studio .NET 2003SDKv1.1Binwincv.exe Wincv works well, but it doesn’t allow me to

By Jeff Atwood ·
Comments

Managed HTML rendering

At some point in any WinForms project, you’re bound to need either: 1. WYSIWYG text entry areas with text formatting 2. Quick and dirty printed report generation The obvious choice for both of these things is HTML. No problem! I’ll just drag my HtmlTextBox on the form, set

By Jeff Atwood ·
Comments

Recent Posts

Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
The Great Filter Comes For Us All

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven’t any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don’t stop there – read the Story of Your Life novella it was based on for so much

By Jeff Atwood ·
Comments
I Fight For The Users

I Fight For The Users

If you haven’t been able to keep up with my blistering pace of one blog post per year, I don’t blame you. There’s a lot going on right now. It’s a busy time. But let’s pause and take a moment to celebrate that Elon Musk

By Jeff Atwood ·
Comments
The 2030 Self-Driving Car Bet

The 2030 Self-Driving Car Bet

It’s my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger use

By Jeff Atwood ·
Comments