{"id":1356,"date":"2023-04-03T12:22:38","date_gmt":"2023-04-03T05:22:38","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1356"},"modified":"2023-04-03T12:25:17","modified_gmt":"2023-04-03T05:25:17","slug":"csharp-rethrow-exception","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-rethrow-exception\/","title":{"rendered":"C# Rethrow Exceptions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to rethrow an exception in C# using the <code>throw<\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When to rethrow exceptions in C# <\/h2>\n\n\n\n<p>In C#, you can use the <code><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-try-catch\/\">try...catch<\/a><\/code> block to catch an exception in the <code>try<\/code> block and handle it accordingly in the <code>catch<\/code> block.<\/p>\n\n\n\n<p>In some cases, you may want to re-throw an exception after you have caught it in the <code>catch<\/code> block.<\/p>\n\n\n\n<p>This can be useful if you need to log the exception first and then propagate it up the call stack, which you will handle in another <code>catch<\/code> block.<\/p>\n\n\n\n<p>The following illustrates how to rethrow an exception:<\/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\">try<\/span>\n{\n    <span class=\"hljs-comment\">\/\/ Some code that might throw an exception<\/span>\n}\n<span class=\"hljs-keyword\">catch<\/span> (Exception ex)\n{\n    <span class=\"hljs-comment\">\/\/ Handle the exception<\/span>\n    Console.WriteLine(<span class=\"hljs-string\">$\"An exception occurred: <span class=\"hljs-subst\">{ex.Message}<\/span>\"<\/span>);\n\n    <span class=\"hljs-comment\">\/\/ Rethrow the exception<\/span>\n    <span class=\"hljs-keyword\">throw<\/span>;\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>In this syntax, the code in the <code>try<\/code> block might throw an exception. If an exception occurs, we handle it in the <code>catch<\/code> block.<\/p>\n\n\n\n<p>In the <code>catch<\/code> block, we handle the exception by showing a message to the console and use rethrow it using the <code>throw<\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C# rethrow exceptions example<\/h2>\n\n\n\n<p>The following example demonstrates how to rethrow an exception using the <code>throw<\/code> statement:<\/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-function\"><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">float<\/span> <span class=\"hljs-title\">Divide<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">int<\/span> a, <span class=\"hljs-keyword\">int<\/span> b<\/span>)<\/span>\n{\n    <span class=\"hljs-keyword\">return<\/span> a \/ b;\n}\n\n<span class=\"hljs-keyword\">try<\/span>\n{\n    <span class=\"hljs-keyword\">var<\/span> result = Divide(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">0<\/span>);\n}\n<span class=\"hljs-keyword\">catch<\/span> (DivideByZeroException ex)\n{\n    Console.WriteLine(ex.Message);\n    <span class=\"hljs-comment\">\/\/ rethrow the exception<\/span>\n    <span class=\"hljs-keyword\">throw<\/span>;\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>How it works.<\/p>\n\n\n\n<p>First, define the <code>Divide()<\/code> method that returns the division of two integers.<\/p>\n\n\n\n<p>Second, divide 10 by zero using the <code>Divide<\/code> method. In the catch block, we log the exception message to the console and rethrow the same <code>DivideByZeroException<\/code> exception.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Catching and rethrowing another exception<\/h2>\n\n\n\n<p>Sometimes, you want to handle an exception and wrap it in a new exception, and propagate the new exception up the call stack.<\/p>\n\n\n\n<p>To do that you use the <code>throw<\/code> statement with the new exception and you pass the original exception to the inner exception parameter of the new exception. For example:<\/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-function\"><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">float<\/span> <span class=\"hljs-title\">Divide<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">int<\/span> a, <span class=\"hljs-keyword\">int<\/span> b<\/span>)<\/span>\n{\n    <span class=\"hljs-keyword\">return<\/span> a \/ b;\n}\n\n\n<span class=\"hljs-keyword\">try<\/span>\n{\n    <span class=\"hljs-keyword\">var<\/span> result = Divide(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">0<\/span>);\n}\n<span class=\"hljs-keyword\">catch<\/span> (DivideByZeroException ex)\n{\n    Console.WriteLine(ex.Message);\n\n    <span class=\"hljs-comment\">\/\/ rethrow the exception<\/span>\n    <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> ArithmeticException(ex.Message, ex);\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>In this example, we log the message of the <code>DivideByZeroException<\/code> exception to the console. <\/p>\n\n\n\n<p>Also, we wrap the <code><code>DivideByZeroException<\/code><\/code> into a new <code><code>ArithmeticException<\/code><\/code> exception by assigning the <code><code>DivideByZeroException<\/code><\/code> exception to the <code>InnerException<\/code> property of the <code><code>ArithmeticException<\/code><\/code> exception.<\/p>\n\n\n\n<p>The following shows the <code>ArithmeticException<\/code> with <code>DivisionByZeroException<\/code> as the inner exception:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"314\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2023\/04\/c-rethrow-exception.png\" alt=\"\" class=\"wp-image-1357\" srcset=\"https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2023\/04\/c-rethrow-exception.png 598w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2023\/04\/c-rethrow-exception-300x158.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Rethrow an exception when you want to do something like logging before propagating the same exception up the call stack.<\/li>\n\n\n\n<li>Use the <code>throw<\/code> statement to rethrow the same exception.<\/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=\"1356\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-rethrow-exception\/\"\n\t\t\t\tdata-post-title=\"C# Rethrow 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=\"1356\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-rethrow-exception\/\"\n\t\t\t\tdata-post-title=\"C# Rethrow 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>Summary: in this tutorial, you&#8217;ll learn how to rethrow an exception in C# using the throw statement. When to rethrow exceptions in C# In C#, you can use the try&#8230;catch block to catch an exception in the try block and handle it accordingly in the catch block. In some cases, you may want to re-throw [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":61,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1356","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1356","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=1356"}],"version-history":[{"count":4,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1356\/revisions"}],"predecessor-version":[{"id":1363,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1356\/revisions\/1363"}],"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=1356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}