<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Maysam Ehab Al-Qrinawi</title>
    <description>The latest articles on DEV Community by Maysam Ehab Al-Qrinawi (@maysam_ehab_).</description>
    <link>https://dev.to/maysam_ehab_</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4078821%2Fc021c59b-c278-447e-a91a-5301b2b840f1.jpg</url>
      <title>DEV Community: Maysam Ehab Al-Qrinawi</title>
      <link>https://dev.to/maysam_ehab_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maysam_ehab_"/>
    <language>en</language>
    <item>
      <title>What I Learned About Exceptions in C#: Understanding Errors Instead of Hiding Them</title>
      <dc:creator>Maysam Ehab Al-Qrinawi</dc:creator>
      <pubDate>Sat, 15 Aug 2026 11:15:26 +0000</pubDate>
      <link>https://dev.to/maysam_ehab_/what-i-learned-about-exceptions-in-c-understanding-errors-instead-of-hiding-them-5b71</link>
      <guid>https://dev.to/maysam_ehab_/what-i-learned-about-exceptions-in-c-understanding-errors-instead-of-hiding-them-5b71</guid>
      <description>&lt;p&gt;When I started learning C#, I thought exceptions were mainly about one thing:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Put the code inside &lt;code&gt;try&lt;/code&gt;, catch the exception, and the problem is solved.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But as I worked more with .NET and started building APIs, I realized that &lt;strong&gt;handling an exception is not the same as hiding it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This article is about what I learned while trying to understand exceptions in C# and how my way of thinking about errors changed.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is an Exception?
&lt;/h2&gt;

&lt;p&gt;An exception is an unexpected situation that happens while a program is running.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code cannot convert &lt;code&gt;"hello"&lt;/code&gt; into an integer, so C# throws a &lt;code&gt;FormatException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trying to access &lt;code&gt;Length&lt;/code&gt; when &lt;code&gt;name&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; can result in a &lt;code&gt;NullReferenceException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These errors are not necessarily bad.&lt;/p&gt;

&lt;p&gt;They are information.&lt;/p&gt;

&lt;p&gt;They tell us:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Something happened that your program needs to deal with."&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  My First Approach: Catch Everything
&lt;/h2&gt;

&lt;p&gt;At first, I thought something like this was a good solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;DoSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application doesn't crash.&lt;/p&gt;

&lt;p&gt;The error disappears.&lt;/p&gt;

&lt;p&gt;So... problem solved?&lt;/p&gt;

&lt;p&gt;Not really.&lt;/p&gt;

&lt;p&gt;The problem is that I didn't actually handle anything.&lt;/p&gt;

&lt;p&gt;I just told the application:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"If something goes wrong, ignore it."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this can make debugging much harder.&lt;/p&gt;

&lt;p&gt;If something fails in production, I might not even know that it happened.&lt;/p&gt;




&lt;h2&gt;
  
  
  So What Should We Do Instead?
&lt;/h2&gt;

&lt;p&gt;The first thing I learned is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't catch an exception unless you know what you are going to do with it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, if I'm parsing user input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FormatException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Please enter a valid number."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, catching &lt;code&gt;FormatException&lt;/code&gt; makes sense.&lt;/p&gt;

&lt;p&gt;I know what the problem means, and I know how to respond to it.&lt;/p&gt;

&lt;p&gt;This is much better than:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Something went wrong."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The more specific the exception, the more meaningful the handling can be.&lt;/p&gt;




&lt;h2&gt;
  
  
  What About &lt;code&gt;ArgumentNullException&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;While learning C#, I also came across exceptions such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;ArgumentNullException&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, I wondered:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why create a specific exception when I can just use **&lt;/strong&gt;&lt;code&gt;Exception&lt;/code&gt;*&lt;em&gt;**?&lt;/em&gt;*&lt;/p&gt;

&lt;p&gt;The answer became clearer when I understood that exceptions communicate the type of problem.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;CreateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Continue...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I'm saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The caller provided a null argument where a value was required."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's much more informative than:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Something went wrong."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exception itself carries useful information about the problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  Throwing an Exception Is Not the Same as Catching It
&lt;/h2&gt;

&lt;p&gt;This was another important thing I learned.&lt;/p&gt;

&lt;p&gt;We can &lt;strong&gt;throw&lt;/strong&gt; an exception when something invalid happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Amount must be greater than zero."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And another layer of the application can decide whether it needs to catch and handle that exception.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Controller
    ↓
Service
    ↓
Business Logic
    ↓
Exception
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The service might detect the problem, but the appropriate place to convert that error into an HTTP response could be higher in the application.&lt;/p&gt;

&lt;p&gt;This made me start thinking about exceptions as part of the application's flow rather than just something that belongs inside a &lt;code&gt;try/catch&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Exceptions in ASP.NET Core
&lt;/h2&gt;

&lt;p&gt;When I started working with ASP.NET Core APIs, this became even more important.&lt;/p&gt;

&lt;p&gt;Imagine an API endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;HttpGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{id}"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IActionResult&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_userService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetByIdAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If something unexpected happens inside the service, I don't necessarily want every controller to contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That would quickly become repetitive.&lt;/p&gt;

&lt;p&gt;Instead, ASP.NET Core applications can use &lt;strong&gt;global exception handling&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, exception-handling middleware can act as a central place to handle unexpected exceptions.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Controller
   ↓
Service
   ↓
Exception
   ↓
Global Exception Handler
   ↓
HTTP Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach helped me understand an important principle:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Handle errors at the right level, not everywhere.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Should We Create Custom Exceptions?
&lt;/h2&gt;

&lt;p&gt;Sometimes the built-in exceptions are enough.&lt;/p&gt;

&lt;p&gt;But in some applications, we may want exceptions that represent specific business problems.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserNotFoundException&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;UserNotFoundException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;UserNotFoundException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"User was not found."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the exception represents a specific situation in the application.&lt;/p&gt;

&lt;p&gt;However, I also learned that &lt;strong&gt;custom exceptions shouldn't be created just because we can&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If an existing .NET exception clearly describes the problem, using it can be simpler and clearer.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Learned About Error Messages
&lt;/h2&gt;

&lt;p&gt;I used to think the exception type was enough.&lt;/p&gt;

&lt;p&gt;But the message matters too.&lt;/p&gt;

&lt;p&gt;Compare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"Amount must be greater than zero."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second one gives much more useful information.&lt;/p&gt;

&lt;p&gt;A good error message should help us understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What went wrong?&lt;/li&gt;
&lt;li&gt;Which value caused the problem?&lt;/li&gt;
&lt;li&gt;What was expected?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But we should also be careful not to expose sensitive information to API users.&lt;/p&gt;




&lt;h2&gt;
  
  
  Exceptions Shouldn't Replace Validation
&lt;/h2&gt;

&lt;p&gt;Another thing I'm learning is that not every invalid input should necessarily become an exception.&lt;/p&gt;

&lt;p&gt;For example, if an API receives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;-5&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is expected invalid user input.&lt;/p&gt;

&lt;p&gt;It may be better to validate the request and return a validation response rather than using exceptions for normal application flow.&lt;/p&gt;

&lt;p&gt;Exceptions are more appropriate for &lt;strong&gt;unexpected or exceptional situations&lt;/strong&gt;, while validation handles expected invalid input.&lt;/p&gt;

&lt;p&gt;This distinction helped me understand exceptions much better.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Would Do Differently Now
&lt;/h2&gt;

&lt;p&gt;If I were starting again, I would remember these rules:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Don't catch exceptions just to make them disappear
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;usually doesn't solve the real problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Catch specific exceptions when you can handle them
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FormatException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Handle invalid format&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is more meaningful.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Use the right built-in exception
&lt;/h3&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;ArgumentNullException&lt;/span&gt;
&lt;span class="n"&gt;ArgumentException&lt;/span&gt;
&lt;span class="n"&gt;InvalidOperationException&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when they accurately describe the situation.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Don't use exceptions for normal validation
&lt;/h3&gt;

&lt;p&gt;Expected invalid input should usually be handled through validation.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Think about where the exception should be handled
&lt;/h3&gt;

&lt;p&gt;Not every exception needs a &lt;code&gt;try/catch&lt;/code&gt; in every method.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Don't expose sensitive internal details
&lt;/h3&gt;

&lt;p&gt;The error returned to the client should not reveal things like database connection strings, stack traces, passwords, or internal implementation details.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Biggest Lesson
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learned is that &lt;strong&gt;exceptions are not just errors that we need to hide&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They are a way for different parts of an application to communicate that something unexpected happened.&lt;/p&gt;

&lt;p&gt;The goal isn't:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"How can I stop this exception from appearing?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The better question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"What does this exception tell me, and where should this problem be handled?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'm still learning .NET, and I'm sure my understanding of exception handling will continue to evolve as I build larger applications.&lt;/p&gt;

&lt;p&gt;But this small change in perspective made exception handling much clearer for me.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;When you're learning programming, it's easy to focus on making the code work.&lt;/p&gt;

&lt;p&gt;But as you build larger applications, you start asking different questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens when something fails?&lt;/li&gt;
&lt;li&gt;Where should I handle it?&lt;/li&gt;
&lt;li&gt;How can I make debugging easier?&lt;/li&gt;
&lt;li&gt;What information should the user receive?&lt;/li&gt;
&lt;li&gt;How can I keep the application maintainable?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For me, understanding exceptions was one of those small topics that turned into a bigger lesson about &lt;strong&gt;writing reliable software&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And I'm still learning. 🚀&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What was the biggest lesson you learned about exception handling in C#?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'd love to hear your thoughts.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
