.NET Tips and Tricks

Blog archive

Reporting Exceptions Well in Visual Studio

Microsoft has some "best practice" advice to share on how to handle exceptions (a topic I've discussed elsewhere). The Microsoft article is well worth reading ... but there's one piece of advice that I disagree with (talk about hubris, eh?).

One of Microsoft's recommended practices is that you should prefer to throw a built-in Exception class rather than your own, custom Exception class. The problem I see with this is that the built-in Exception objects return, at best, the technical reason for the exception (for example, "Division by zero"). Creating your own exception object allows you to specify the business reason for the failure (for example, "Customer has no sales orders").

Personally, I think that the message with the business reason is more useful both to the user faced with the message and the developer who has to fix the problem. Instead, I recommend using a custom exception object and tucking the original Exception into your custom Exception object's InnerException property. That gives you the best of both worlds.

Posted by Peter Vogel on 04/26/2019


comments powered by Disqus

Featured

  • ASP.NET Core and Blazor Dominate 'Relatively Small' .NET 6 Preview 6

    In what Microsoft called a “relatively small” release, .NET 6 Preview 6 delivered its biggest set of updates to ASP.NET Core and Blazor. New features include required component parameters, JavaScript interop enhancements, and support for state persistence in Blazor Server apps. Other areas like .NET MAUI and EF Core saw targeted improvements focused on UI styling, accessibility, and standardized configuration conventions. Visual Studio 2022 Preview 2 is now officially supported, enabling key tools like .NET MAUI development and C# Hot Reload.

  • GitHub Copilot Is Rising All-Time Contributor to .NET MAUI Repo

    The .NET MAUI team is pursuing a dream scenario where GitHub Copilot handles a straightforward issue all by itself with just minimal initial instructions.

  • Kernel Ridge Regression with Stochastic Gradient Descent Training Using C#

    Dr. James McCaffrey presents a complete end-to-end demonstration of the kernel ridge regression technique to predict a single numeric value. The demo uses stochastic gradient descent, one of two possible training techniques. There is no single best machine learning regression technique. When kernel ridge regression prediction works, it is often highly accurate.

  • VS Code v1.102 Doubles Down on GitHub Copilot and AI-Enhanced Development

    VS Code’s June 2025 update supercharges GitHub Copilot with new chat customization tools, smarter inline completions, and an open-source Copilot Chat extension that makes AI-assisted coding more transparent and flexible.

  • Azure Vibe Coding for the Enterprise Masses: Microsoft Partners with Replit

    Replit has partnered with Microsoft to bring its AI-powered, natural language coding platform to Azure, enabling enterprise workers to build and deploy software without writing code—marking a major step toward agentic, no-code application development at scale.

Subscribe on YouTube