Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix a Tuple Deconstruction Hiding a Discarded Exception

- 30.11.26 - ErcanOPAK

🔧 Fix a Tuple Deconstruction Hiding a Discarded Exception

A method that returns a value tuple paired with a ‘try’ style boolean ((bool Success, T Value)) is a common pattern for avoiding exceptions on the happy path, but deconstructing straight into two variables and only checking the boolean means any exception the inner implementation swallowed to produce that boolean in the first place is gone for good — there’s no stack trace, no message, nothing left to debug once the call site only sees (false, default).

🔎 The Problem

public (bool Success, Config Value) TryLoad(string path)
{
    try { return (true, Parse(File.ReadAllText(path))); }
    catch { return (false, default); } // original exception discarded
}

var (ok, cfg) = TryLoad(path);
if (!ok) { Log("Load failed"); } // no idea WHY it failed

✅ Fix: Carry the Failure Reason Through, Not Just a Boolean

  • Extend the tuple to include the caught exception or a message ((bool Success, T Value, Exception? Error)) so the call site can log or inspect the real cause instead of a bare false.
  • At minimum, log the caught exception at the point it’s caught, even if the public-facing method still only returns a boolean to its caller — that preserves the diagnostic information somewhere.
  • Reserve the ‘try’ tuple pattern for genuinely expected failure cases (file not found, parse error) and let truly unexpected exceptions propagate normally, so a silent catch-all doesn’t end up hiding bugs that should have been loud.

A boolean can tell you that something failed — it can’t tell you why, and the exception that knew why is already gone.

— C# Best Practice

Related posts:

C#: Use File-Scoped Namespaces to Remove One Level of Indentation

C#: Use File-Scoped Namespaces to Remove One Level of Indentation

How to add placeholder to Multiple Selection DropDownList in Asp.Net MVC

How to add placeholder to Multiple Selection DropDownList in Asp.Net MVC

C#: Master Exception Handling for Robust Applications

C#: Master Exception Handling for Robust Applications

Views: 14
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

SQL Server: Fix Parameter Sniffing on a Seasonal Reporting Query
Windows 11: Fix a Second Monitor Flickering Only at 120Hz

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 996 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 995 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 866 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com