{"id":1370,"date":"2023-04-04T10:18:04","date_gmt":"2023-04-04T03:18:04","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1370"},"modified":"2023-04-04T10:28:49","modified_gmt":"2023-04-04T03:28:49","slug":"csharp-custom-exceptions","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-custom-exceptions\/","title":{"rendered":"C# Custom Exceptions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about C# custom exceptions and how to create and use a custom exception in your program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# custom exceptions<\/h2>\n\n\n\n<p>C# provides you with many built-in exception classes. Sometimes, these built-in exception classes are not sufficient to describe the error in your application. In this case, you can define custom exception classes.<\/p>\n\n\n\n<p>By definition, a custom exception is an exception that you create to represent a specific error in your application.<\/p>\n\n\n\n<p>To create a custom exception, you define a class that inherits from the <code>System.Exception<\/code> class. By convention, an exception class ends with <code>Exception<\/code> like <code>MyCustomException<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyCustomException<\/span>: <span class=\"hljs-title\">Exception<\/span>\n{\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>A custom exception class should have three standard constructors:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyCustomException<\/span>: <span class=\"hljs-title\">Exception<\/span>\n{\n    MyCustomException(): <span class=\"hljs-keyword\">base<\/span>() { }\n    MyCustomException(<span class=\"hljs-keyword\">string<\/span> message) : <span class=\"hljs-keyword\">base<\/span>(message) { }\n    MyCustomException(<span class=\"hljs-keyword\">string<\/span> message, Exception innerException) : <span class=\"hljs-keyword\">base<\/span>(message, innerException) { }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first constructor is a default constructor that takes no arguments. <\/li>\n\n\n\n<li>The second constructor takes a string message that describes the exception. <\/li>\n\n\n\n<li>The third constructor takes both a message and an inner exception, which you can use to chain exceptions together to describe the error in more detail.<\/li>\n<\/ul>\n\n\n\n<p>A custom exception class may also have additional properties to provide more information about the error that occurs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C# custom exception example<\/h2>\n\n\n\n<p>Let&#8217;s take a look at an example of using a custom exception.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Account<\/span>\n{\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">decimal<\/span> balance;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">decimal<\/span> Balance =&gt; balance;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Account<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> initialBalance = <span class=\"hljs-number\">0<\/span><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">if<\/span> (initialBalance &lt; <span class=\"hljs-number\">0<\/span>)\n        {\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> ArgumentOutOfRangeException(<span class=\"hljs-string\">$\"The <span class=\"hljs-subst\">{<span class=\"hljs-keyword\">nameof<\/span>(initialBalance)}<\/span> must be zero or positive.\"<\/span>);\n        }\n        balance = initialBalance;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Account <span class=\"hljs-title\">Withdraw<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> amount<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">if<\/span> (amount &gt; Balance)\n        {\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> ArgumentOutOfRangeException(\n                <span class=\"hljs-keyword\">nameof<\/span>(amount),\n                <span class=\"hljs-string\">$\"Could not withdraw an amount (<span class=\"hljs-subst\">{amount:C}<\/span>) that is more than balance (<span class=\"hljs-subst\">{Balance:C}<\/span>)\"<\/span>\n            );\n        }\n\n        balance -= amount;\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>;\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Account <span class=\"hljs-title\">Deposit<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> amount<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">if<\/span> (amount &lt;= <span class=\"hljs-number\">0<\/span>)\n        {\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> ArgumentOutOfRangeException(<span class=\"hljs-keyword\">nameof<\/span>(amount));\n        }\n        balance += amount;\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>;\n    }\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n\n        <span class=\"hljs-keyword\">var<\/span> account = <span class=\"hljs-keyword\">new<\/span> Account(<span class=\"hljs-number\">100<\/span>);\n        <span class=\"hljs-keyword\">try<\/span>\n        {\n            account.Withdraw(<span class=\"hljs-number\">200<\/span>);\n        }\n        <span class=\"hljs-keyword\">catch<\/span> (ArgumentOutOfRangeException ex)\n        {\n            WriteLine(ex.Message);\n        }\n        <span class=\"hljs-keyword\">catch<\/span> (Exception ex)\n        {\n            WriteLine(ex.Message);\n        }\n\n        ReadLine();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\">Could not withdraw an <span class=\"hljs-title\">amount<\/span> (<span class=\"hljs-params\">$<span class=\"hljs-number\">200.00<\/span><\/span>) that exceeds the <span class=\"hljs-title\">balance<\/span> (<span class=\"hljs-params\">$<span class=\"hljs-number\">100.00<\/span><\/span>) (<span class=\"hljs-params\">Parameter <span class=\"hljs-string\">'amount'<\/span><\/span>)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define an <code>Account<\/code> class that represents a bank account with a balance. The <code>Account<\/code> class has two methods for depositing and withdrawing money. These methods use a built-in exception class <code>ArgumentOutOfRangeException<\/code> to prevent invalid operations.<\/p>\n\n\n\n<p>Second, create a new <code>Account<\/code> object in the <code>Main()<\/code> method of the <code>Program<\/code> class with an initial balance of 100. Then, attempt to withdraw 200 from the account using the <code>Withdraw()<\/code> method. <\/p>\n\n\n\n<p>Because the balance is insufficient, the <code>Withdraw()<\/code> method throws an <code>ArgumentOutOfRangeException<\/code>. <\/p>\n\n\n\n<p>The catch block handles the <code>ArgumentOutOfRangeException<\/code> exception by displaying the error message on the console.<\/p>\n\n\n\n<p>The program works fine except that the <code>ArgumentOutOfRangeException<\/code> is not very expressive and the error message doesn&#8217;t show the deficit.<\/p>\n\n\n\n<p>To improve this, we can create a new custom exception as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span> : <span class=\"hljs-title\">Exception<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">decimal<\/span> Deficit\n    {\n        <span class=\"hljs-keyword\">get<\/span>;\n        <span class=\"hljs-keyword\">set<\/span>;\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span>(<span class=\"hljs-params\"><\/span>)\n        : <span class=\"hljs-title\">base<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span>(<span class=\"hljs-params\">\n        <span class=\"hljs-keyword\">string<\/span> message\n    <\/span>)\n        : <span class=\"hljs-title\">base<\/span>(<span class=\"hljs-params\">message<\/span>)<\/span>\n    {\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span>(<span class=\"hljs-params\">\n      <span class=\"hljs-keyword\">string<\/span> message,\n      Exception innerException\n  <\/span>) : <span class=\"hljs-title\">base<\/span>(<span class=\"hljs-params\">message, innerException<\/span>)<\/span>\n    {\n\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> deficit<\/span>)<\/span>\n    {\n        Deficit = deficit;\n    }\n\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">string<\/span> Message =&gt;\n         <span class=\"hljs-string\">$\"<span class=\"hljs-subst\">{<span class=\"hljs-keyword\">base<\/span>.Message}<\/span> Could not withdraw due to a deficit of <span class=\"hljs-subst\">{Deficit:C}<\/span>\"<\/span>;\n\n\n}\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Account<\/span>\n{\n\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">decimal<\/span> balance;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">decimal<\/span> Balance =&gt; balance;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Account<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> initialBalance = <span class=\"hljs-number\">0<\/span><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">if<\/span> (initialBalance &lt; <span class=\"hljs-number\">0<\/span>)\n        {\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> ArgumentOutOfRangeException(<span class=\"hljs-string\">$\"The <span class=\"hljs-subst\">{<span class=\"hljs-keyword\">nameof<\/span>(initialBalance)}<\/span> must be zero or positive.\"<\/span>);\n        }\n        balance = initialBalance;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Account <span class=\"hljs-title\">Withdraw<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> amount<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">if<\/span> (amount &gt; Balance)\n        {\n            <span class=\"hljs-keyword\">var<\/span> deficit = amount - Balance;\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> InsufficientFundException(deficit);\n\n        }\n\n        balance -= amount;\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>;\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Account <span class=\"hljs-title\">Deposit<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> amount<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">if<\/span> (amount &lt;= <span class=\"hljs-number\">0<\/span>)\n        {\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> ArgumentOutOfRangeException(<span class=\"hljs-keyword\">nameof<\/span>(amount));\n        }\n        balance += amount;\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>;\n    }\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n\n        <span class=\"hljs-keyword\">var<\/span> account = <span class=\"hljs-keyword\">new<\/span> Account(<span class=\"hljs-number\">100<\/span>);\n        <span class=\"hljs-keyword\">try<\/span>\n        {\n            account.Withdraw(<span class=\"hljs-number\">200<\/span>);\n        }\n        <span class=\"hljs-keyword\">catch<\/span> (InsufficientFundException ex)\n        {\n            WriteLine(ex.Message);\n        }\n        <span class=\"hljs-keyword\">catch<\/span> (Exception ex)\n        {\n            WriteLine(ex.Message);\n        }\n\n        ReadLine();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">Exception of type <span class=\"hljs-string\">'InsufficientFundException'<\/span> was thrown. Could not withdraw due to a deficit of $<span class=\"hljs-number\">100.00<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a new custom exception class named <code>InsufficientFundException<\/code> that inherits from the <code>Exception<\/code> class. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span> : <span class=\"hljs-title\">Exception<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">decimal<\/span> Deficit\n    {\n        <span class=\"hljs-keyword\">get<\/span>;\n        <span class=\"hljs-keyword\">set<\/span>;\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span>(<span class=\"hljs-params\"><\/span>)\n        : <span class=\"hljs-title\">base<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span>(<span class=\"hljs-params\">\n        <span class=\"hljs-keyword\">string<\/span> message\n    <\/span>)\n        : <span class=\"hljs-title\">base<\/span>(<span class=\"hljs-params\">message<\/span>)<\/span>\n    {\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span>(<span class=\"hljs-params\">\n      <span class=\"hljs-keyword\">string<\/span> message,\n      Exception innerException\n  <\/span>) : <span class=\"hljs-title\">base<\/span>(<span class=\"hljs-params\">message, innerException<\/span>)<\/span>\n    {\n\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">InsufficientFundException<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> deficit<\/span>)<\/span>\n    {\n        Deficit = deficit;\n    }\n\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">string<\/span> Message =&gt;\n         <span class=\"hljs-string\">$\"<span class=\"hljs-subst\">{<span class=\"hljs-keyword\">base<\/span>.Message}<\/span> Could not withdraw due to a deficit of <span class=\"hljs-subst\">{Deficit:C}<\/span>\"<\/span>;\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>InsufficientFundException<\/code> class implements three standard constructors. <\/p>\n\n\n\n<p>The <code>InsufficientFundException<\/code> class also has a fourth constructor that takes a decimal parameter called <code>deficit<\/code>, which represents the deficit when attempting to withdraw an amount that exceeds the balance. This constructor sets the <code>Deficit<\/code> property to the deficit parameter.<\/p>\n\n\n\n<p>The <code>InsufficientFundException<\/code> class extends the <code>Message<\/code> property of the <code>Exception<\/code> class. It concatenates the <code>Message<\/code> of the base class with a custom message that includes the <code>deficit<\/code>.<\/p>\n\n\n\n<p>In the <code>Withdraw()<\/code> method of the <code>Account<\/code> class, instead of throwing the <code>ArgumentOutOfRangeException<\/code>, it throws the <code>InsufficientFundException<\/code> exception and pass the <code>deficit<\/code> to its constructor.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Account <span class=\"hljs-title\">Withdraw<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">decimal<\/span> amount<\/span>)<\/span>\n{\n    <span class=\"hljs-keyword\">if<\/span> (amount &gt; Balance)\n    {\n        <span class=\"hljs-keyword\">var<\/span> deficit = amount - Balance;\n        <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> InsufficientFundException(deficit);\n    }\n\n    balance -= amount;\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>Main<\/code> method of the <code>Program<\/code> class, instead of catching the <code><code>ArgumentOutOfRangeException<\/code><\/code> exception, we catch the <code><code>ArgumentOutOfRangeException<\/code><\/code> and display the error message on the console:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">var<\/span> account = <span class=\"hljs-keyword\">new<\/span> Account(<span class=\"hljs-number\">100<\/span>);\n        <span class=\"hljs-keyword\">try<\/span>\n        {\n            account.Withdraw(<span class=\"hljs-number\">200<\/span>);\n        }\n        <span class=\"hljs-keyword\">catch<\/span> (ArgumentOutOfRangeException ex)\n        {\n            WriteLine(ex.Message);\n        }\n        <span class=\"hljs-keyword\">catch<\/span> (Exception ex)\n        {\n            WriteLine(ex.Message);\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Only create a custom class exception class if it provides more meaningful and detailed information about the error in your application.<\/li>\n\n\n\n<li>Extend the built-in <code>System.Exception<\/code> class to define a custom exception class.<\/li>\n\n\n\n<li>Throw a custom exception by using the <code>throw<\/code> keyword followed by the custom exception object.<\/li>\n\n\n\n<li>Use the <code>try...catch<\/code> block to catch a custom exception by specifying the custom exception type in the <code>catch<\/code> block.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1370\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-custom-exceptions\/\"\n\t\t\t\tdata-post-title=\"C# Custom Exceptions\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1370\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-custom-exceptions\/\"\n\t\t\t\tdata-post-title=\"C# Custom Exceptions\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about C# custom exceptions and how to create and use a custom exception in your program.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":62,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1370","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=1370"}],"version-history":[{"count":4,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1370\/revisions"}],"predecessor-version":[{"id":1375,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1370\/revisions\/1375"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=1370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}