<?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: Imam Dahir Dan-Azumi</title>
    <description>The latest articles on DEV Community by Imam Dahir Dan-Azumi (@eimaam).</description>
    <link>https://dev.to/eimaam</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%2F903675%2F1d1997d2-1a94-45e9-86e7-85e0cbe9329e.jpeg</url>
      <title>DEV Community: Imam Dahir Dan-Azumi</title>
      <link>https://dev.to/eimaam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eimaam"/>
    <language>en</language>
    <item>
      <title>Try... Catch...? Finally...</title>
      <dc:creator>Imam Dahir Dan-Azumi</dc:creator>
      <pubDate>Mon, 24 Jul 2023 00:01:03 +0000</pubDate>
      <link>https://dev.to/eimaam/try-catch-finally-1f3f</link>
      <guid>https://dev.to/eimaam/try-catch-finally-1f3f</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Whenever the phrase &lt;em&gt;"Try...? Catch..? Finally..."&lt;/em&gt; pops up in development discussions, I can't help but read it out humorously, as if someone is playfully asking, "Try this… caught anything? Finally, do this to it" (laughs). &lt;br&gt;
However, aside from my tagging it as humorous, this phrase encapsulates a very useful, powerful, and strong concept in JavaScript development. The phrase represents a remarkable and exceptional way of fortifying (if I can use that word :) ) our codes against errors. In this piece, we embark on an all-expense-paid trip (laughs) to explore and comprehend how the trio of try-catch-finally blocks is significant in development. &lt;br&gt;
We will delve into how they effectively empower developers to handle exceptions, their role in effective error handling, and why it's surprising how often they are underutilized in development.&lt;/p&gt;
&lt;h2&gt;
  
  
  What are try-catch-finally blocks?
&lt;/h2&gt;

&lt;p&gt;A try-catch-finally block is a construct in Javascript that provides developers a robust mechanism to better handle exceptions (errors) in their code, particularly during operations like API calls. The try block contains code that might throw exceptions, whereas the catch block gracefully captures and handles these exceptions. The optional finally block, on the other hand, executes the code in its block regardless of the outcome of either of the blocks. With that, a Developer has a better structure and a controlled way to handle exceptions/errors and do necessary cleanup operations.&lt;/p&gt;
&lt;h3&gt;
  
  
  The &lt;code&gt;try&lt;/code&gt; block
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;// try something - like getting list of users&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;catch&lt;/code&gt; block
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="c1"&gt;// caught some errors? do something with it here - log or maybe send a custom toast notification&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;finally&lt;/code&gt; block
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// do something after try and catch block regardless of the response from there.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Sample Code: The &lt;code&gt;trio&lt;/code&gt; in action
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchDataFromAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&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="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to fetch data from the API.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="c1"&gt;// Process the data and return the result.&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&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="nx"&gt;error&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 the exception/error gracefully.&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error occurred while fetching data:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// Optionally, you can handle the error in other ways, such as showing a user-friendly message via a toast notification package&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Perform cleanup operations, if any, regardless of the outcome of try or catch blocks.&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;API call completed.&lt;/span&gt;&lt;span class="dl"&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;h2&gt;
  
  
  Importance of Using &lt;code&gt;try-catch-finally&lt;/code&gt; blocks
&lt;/h2&gt;

&lt;p&gt;The try-catch block plays a vital role in JavaScript development for numerous reasons. Firstly, as earlier mentioned, it creates a sophisticated and effective way of identifying and handling errors/exceptions by wrapping error-prone code with the blocks, thereby shielding developers from code crashes during unexpected situations, ensuring users are not left frustrated. Additionally, the try-catch block contributes to the stability of applications. Aside from taking away the possibility of application crashes, it enables the handling of specific types of exceptions and renders appropriate error handling techniques. That includes giving meaningful error messages to users and logging issues for easier debugging. Frameworks like React greatly benefit from try-catch-finally blocks, especially when executing fetch requests alongside the built-in React state manager like useState. Furthermore, the optional finally block proves valuable in resetting loading states, preventing the rendering of unused states, and enhancing performance.&lt;/p&gt;

&lt;p&gt;A very good example is in resetting loading states in React in order to stop the rendering of unused states:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchDataFromAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&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="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to fetch data from the API.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="c1"&gt;// Process the data and return the result.&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&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="nx"&gt;error&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 the exception/error gracefully.&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error occurred while fetching data:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// Optionally, you can handle the error in other ways, such as showing a user-friendly message via a toast notification package&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Perform cleanup operations, if any, regardless of the outcome of try or catch blocks.&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;API call completed.&lt;/span&gt;&lt;span class="dl"&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;The above example is a component for rendering a Dashboard - the sample function is meant to fetch a list of Users and save it to a &lt;code&gt;state&lt;/code&gt; named &lt;code&gt;users&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function starts with setting the &lt;code&gt;loading&lt;/code&gt; state to &lt;code&gt;true&lt;/code&gt; thereby triggering the &lt;code&gt;loading animation&lt;/code&gt; &lt;code&gt;&amp;lt;MoonLoader /&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;At the &lt;code&gt;catch&lt;/code&gt; block we get hold of exceptions or errors and right now we only &lt;code&gt;log&lt;/code&gt; it to the browser console.&lt;/li&gt;
&lt;li&gt;At the finally block, the &lt;code&gt;loading&lt;/code&gt; state from React's &lt;code&gt;useState&lt;/code&gt; is reset to &lt;code&gt;false&lt;/code&gt; and this is regardless of the response in &lt;code&gt;try or catch&lt;/code&gt; blocks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common mistakes and misconceptions:
&lt;/h2&gt;

&lt;p&gt;Despite its importance, many developers have failed to utilize the try-catch-finally construct effectively. One common mistake is having excessive code within the blocks. It is very important to identify specific sections of your code that need the implementation of the blocks. Another misconception is neglecting the optional block in the trio, the finally block. Developers often forget to perform cleanup operations and how vital the finally block is at executing such seamlessly, such as reallocating memory, resetting loading states, or closing opened states amongst other things to avoid leaks and improve performance.&lt;br&gt;
Let's consider a real-world example like the code above, if that is a Dashboard component for your Web application that makes an API call to get and render list of Users in your platform. By using try-catch-finally blocks, you can gracefully handle scenarios where the API request fails due to network issues. You can display a user-friendly error message to the user, log the error for troubleshooting, and ensure that the application remains functional despite the failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Try-Catch-Finally constructs are important tools in error handling, avoiding leaks and improving performances in JavaScript development. They give developers an easy to implement ground for handling exceptions, catching specific error messages and handling cleanup operations. By following the best practices and utilizing these constructs effectively, Developers can ensure robustness &amp;amp; reliability of their code. Embrace that power and let your code glitter! &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
