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?

Jeff Atwood

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Let's be kind to each other. Find me https://infosec.exchange/@codinghorror

⏲️ Busy signing you up.

❗ Something's gone wrong. Please try again.

✅ Success! Check your inbox (and your spam folder, just in case).

Related posts

Computers are Lousy Random Number Generators

Computers are Lousy Random Number Generators

The .NET framework provides two random number generators. The first is System.Random. 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 definite mathematical algorithm is used to select them, but they

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

map of the United States via rgmii.org showing all 3,143 counties by rural (gold) / metro (grey) and population

Launching The Rural Guaranteed Minimum Income Initiative

It's been a year since I invited Americans to join us in a pledge to Share the American Dream: 1. Support organizations you feel are effectively helping those most in need across America right now. 2. Within the next five years, also contribute public dedications of time or

By Jeff Atwood ·
Comments
Let's Talk About The American Dream

Let's Talk About The American Dream

A few months ago I wrote about what it means to stay gold — to hold on to the best parts of ourselves, our communities, and the American Dream itself. But staying gold isn’t passive. It takes work. It takes action. It takes hard conversations that ask us to confront

By Jeff Atwood ·
Comments
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
I’m feeling unlucky... 🎲   See All Posts