<?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: Taizy</title>
    <description>The latest articles on DEV Community by Taizy (@snrelghgub).</description>
    <link>https://dev.to/snrelghgub</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F453951%2F798a49b7-c140-49e6-9330-0e714fab7541.png</url>
      <title>DEV Community: Taizy</title>
      <link>https://dev.to/snrelghgub</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/snrelghgub"/>
    <language>en</language>
    <item>
      <title>Learn C#: Handle Exceptions for More Reliable Code</title>
      <dc:creator>Taizy</dc:creator>
      <pubDate>Wed, 30 Aug 2023 14:11:25 +0000</pubDate>
      <link>https://dev.to/snrelghgub/learn-c-handle-exceptions-for-more-reliable-code-a99</link>
      <guid>https://dev.to/snrelghgub/learn-c-handle-exceptions-for-more-reliable-code-a99</guid>
      <description>&lt;p&gt;Exception handling is a critical skill for any developer aiming to write robust &amp;amp; reliable code, particularly in scenarios involving HTTP requests. Whether you're working on a .NET based project or any other type of C# application, understanding how to handle exceptions in the context of HTTP requests is essential. In this blog, we'll explore how to use the try-catch block to manage exceptions effectively when making an HTTP request using an example.&lt;/p&gt;

&lt;p&gt;In the following example, we use the try-catch block to handle exceptions when making an HTTP request using the &lt;code&gt;HttpClient&lt;/code&gt; class. The code attempts to send an HTTP GET request to an invalid URL. If the response status code indicates a failure, a &lt;code&gt;HttpRequestException&lt;/code&gt; is thrown with a descriptive error message. Otherwise, if the response is successful, a success message is printed to the console.&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="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpClient&lt;/span&gt; &lt;span class="n"&gt;httpClient&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;())&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;invalidUrl&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://example.com/nonexistent-page"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;HttpResponseMessage&lt;/span&gt; &lt;span class="n"&gt;response&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;httpClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;invalidUrl&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsSuccessStatusCode&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;HttpRequestException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Failed to fetch data.\nStatus code: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&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;"Request was successful!"&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;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpRequestException&lt;/span&gt; &lt;span class="n"&gt;ex&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;$"Caught an HttpRequestException: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ex&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="s"&gt;"&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="n"&gt;ex&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;$"Caught an unexpected exception: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ex&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="s"&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;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5KakO6lp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzkz0zh0ghwsj01f8m4j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5KakO6lp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzkz0zh0ghwsj01f8m4j.png" alt="Image description" width="495" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I encourage you to replace the &lt;code&gt;invalidUrl&lt;/code&gt; with a valid URL to witness a successful request. This will help you observe how the try-catch block manages exceptions &amp;amp; how the code behaves when dealing with different HTTP responses.&lt;/p&gt;

&lt;p&gt;If you're looking to enhance your understanding of C# for other similar programming concepts, I invite you to explore my 'Learn C#' GitHub repository at: &lt;a href="https://github.com/snrelghgub/LearnCSharp"&gt;https://github.com/snrelghgub/LearnCSharp&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>programming</category>
      <category>coding</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>My First Day of #1000daysofcoding </title>
      <dc:creator>Taizy</dc:creator>
      <pubDate>Mon, 17 Aug 2020 08:33:11 +0000</pubDate>
      <link>https://dev.to/snrelghgub/my-first-day-of-1000daysofcoding-4edm</link>
      <guid>https://dev.to/snrelghgub/my-first-day-of-1000daysofcoding-4edm</guid>
      <description>&lt;p&gt;/* Today, I decided to begin a #1000daysofcoding challenge instead of the shorter #100daysofcode challenge. It just feels right to me especially after I’ve read, learnt &amp;amp; somewhat already applied some of the valuable things taught in the book ‘The 10X Rule’ by Grant Cardone */&lt;/p&gt;

&lt;p&gt;/* As a matter of fact, I finished a small side project to revise &amp;amp; apply amongst many other things my understanding some Javascript (ES6) basics as taught on freeCodeCamp and learnt 1) how to export &amp;amp; import named functions &amp;amp; variables from module scripts &amp;amp; 2) how to anonymously export a default function or variable &amp;amp; also learnt that, for instance, curly braces { } for this type of import statement is optional */&lt;/p&gt;

&lt;p&gt;you can view my code here: &lt;a href="https://codesandbox.io/s/js-import-export-defaultexport-qt73b"&gt;https://codesandbox.io/s/js-import-export-defaultexport-qt73b&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find me on Twitter :) &lt;br&gt;
@snrelghtwt&lt;/p&gt;

</description>
      <category>1000daysofcoding</category>
      <category>challenge</category>
      <category>coding</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
