<?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: Leah</title>
    <description>The latest articles on DEV Community by Leah (@leah_massey).</description>
    <link>https://dev.to/leah_massey</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%2F2059210%2Fa89e0abd-2174-46a6-ba96-8eec2e8d3dc4.png</url>
      <title>DEV Community: Leah</title>
      <link>https://dev.to/leah_massey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leah_massey"/>
    <language>en</language>
    <item>
      <title>The Four Dots of Mystery</title>
      <dc:creator>Leah</dc:creator>
      <pubDate>Tue, 17 Jun 2025 10:18:08 +0000</pubDate>
      <link>https://dev.to/leah_massey/the-four-dots-of-mystery-3cga</link>
      <guid>https://dev.to/leah_massey/the-four-dots-of-mystery-3cga</guid>
      <description>&lt;p&gt;Every so often, you might come across the strange sight of &lt;code&gt;::&lt;/code&gt; in your Kotlin code.&lt;/p&gt;

&lt;p&gt;This cheeky little four-eyed fella is the Callable Reference Operator. Implementing it creates a &lt;strong&gt;callable reference&lt;/strong&gt; from a function, property or class. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is a callable reference?
&lt;/h3&gt;

&lt;p&gt;A callable reference is a separate object that &lt;em&gt;represents&lt;/em&gt; a function, property or class. It does not execute immediately, rather it holds the reference until the moment that it is invoked. &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 kotlin"&gt;&lt;code&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;calculateNow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// 👆🏻 this variable makes a direct call to the 'add' function. &lt;/span&gt;
&lt;span class="c1"&gt;// It will execute immediately&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;calculateLater&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;
&lt;span class="c1"&gt;// 👆🏻 this variable makes a callable reference of the 'add' function.&lt;/span&gt;
&lt;span class="c1"&gt;// It will only execute when the variable is invoked (as below 👇🏻)&lt;/span&gt;

&lt;span class="nf"&gt;calculateLater&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  The Kotlin.reflect API
&lt;/h3&gt;

&lt;p&gt;The reference we create by using the Callable Reference Operator is always an object from the &lt;strong&gt;kotlin.reflect API&lt;/strong&gt; (eg. an instance of KFunction, KProperty, or KClass). We can see this when we hover over our callable reference in the IDE: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtioclyb9z526b5mkjrg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtioclyb9z526b5mkjrg.png" alt=" " width="607" height="97"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;KFunction2&lt;/code&gt; tells us we are representing a function that has two parameters.&lt;br&gt;
&lt;code&gt;&amp;lt;Int, Int, Int&amp;gt;&lt;/code&gt; tells us the parameters are of type Int, as is the return type.&lt;/p&gt;


&lt;h3&gt;
  
  
  When might we use the Callable Reference Operator?
&lt;/h3&gt;

&lt;p&gt;A callable reference is 'first-class' which means that it can be passed as an argument, stored in a variable, or returned from a function - just as any other data type - a Boolean, String, Int, etc.. This makes it particularly useful for passing into higher order functions. &lt;/p&gt;


&lt;h3&gt;
  
  
  The Callable Reference Operator in action
&lt;/h3&gt;

&lt;p&gt;In the below examples, I demonstrate how the Callable Reference Operator can be used as a concise alternative to passing lambdas to functions in a few different case scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A function reference (::functionName)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [1,3]&lt;/span&gt;

&lt;span class="c1"&gt;// the above line does the same as 👇🏻&lt;/span&gt;

&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// [1,3]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;A property reference (ClassName::propertyName)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;strings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"bc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"def"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 3]&lt;/span&gt;

&lt;span class="c1"&gt;// the above line does the same as 👇🏻&lt;/span&gt;

&lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&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;span class="c1"&gt;// [1, 2, 3]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Constructor reference (::ClassName)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;
&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;personGenerator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; 

&lt;span class="c1"&gt;// the above line does the same as 👇🏻 &lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;personGeneratorUsingLambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;nameParam&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ageParam&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; 
        &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameParam&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ageParam&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// we can also treat ::Person as a first class value in the following higher-order function &lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createPeopleFromData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Pair&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;personGenerator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;personGenerator&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;age&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;listOfPeople&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"Sidney"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Alf"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Morag"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;people&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createPeopleFromData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listOfPeople&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// the above line does the same as 👇🏻 &lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;peopleUsingLambda&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createPeopleFromData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listOfPeopleData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;nameParam&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ageParam&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameParam&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ageParam&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;In this last example, you can really see how the code becomes more concise when we use the Callable Reference Operator. &lt;/p&gt;

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

&lt;p&gt;Using the Callable Reference Operator is pretty neat. However, a clever, concise way of programming is only useful if it's also easy to understand for any other engineer who might come across it. &lt;/p&gt;

&lt;p&gt;I don't come across the Callable Reference Operator very much in production code which implies one of two things: either there is a lack of knowledge / confidence in using it or that the knowledge is there but that engineers find it less intuitive or think it makes code less readable. &lt;/p&gt;

&lt;p&gt;Personally, I don't currently read the Callable Reference Operator as fluently as I might read a lambda. But this will no doubt start to even out as I gain experience around using (and reading) this operator. I will certainly be looking out for it more in code and also look forward to utilising it more in my own work.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Kotlin Exception Handling: The Basics</title>
      <dc:creator>Leah</dc:creator>
      <pubDate>Mon, 07 Apr 2025 20:45:14 +0000</pubDate>
      <link>https://dev.to/leah_massey/exception-handling-4hcf</link>
      <guid>https://dev.to/leah_massey/exception-handling-4hcf</guid>
      <description>&lt;p&gt;In this post, I'll be taking a look at code Exceptions in Kotlin - what they are, where they come from and how we can handle them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
Throwable Superclass
&lt;/li&gt;
&lt;li&gt;
Errors
&lt;/li&gt;
&lt;li&gt;
Exceptions
&lt;/li&gt;
&lt;li&gt;
Catching Exceptions
&lt;/li&gt;
&lt;li&gt;
Throwing Exceptions
&lt;/li&gt;
&lt;li&gt;
Centralised Error Handling &lt;/li&gt;
&lt;li&gt;
Custom Exceptions &lt;/li&gt;
&lt;li&gt;
Stack Trace
&lt;/li&gt;
&lt;li&gt;
Things to Consider &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First off, let's get some context and look at where the Exception class comes from:&lt;/p&gt;

&lt;h2&gt;
  
  
  👑 Throwable Superclass
&lt;/h2&gt;

&lt;p&gt;Exceptions are a subclass of the superclass Throwable.&lt;/p&gt;

&lt;p&gt;For languages such as Kotlin that run on the JVM, the class &lt;code&gt;Throwable&lt;/code&gt; represents anything that is unusual or exceptional in your application. &lt;/p&gt;

&lt;p&gt;Exceptions and Errors are the only direct subclasses of Throwable - however, many more subclasses stem from these two, and developers also have the opportunity to create their own, custom subclasses, too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fww12jodtgv92wvccpere.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fww12jodtgv92wvccpere.jpg" alt=" " width="800" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors V Exceptions - what's the difference?
&lt;/h2&gt;

&lt;p&gt;In a nutshell: &lt;br&gt;
&lt;strong&gt;Errors&lt;/strong&gt; occur when something goes wrong with the Java Virtual Machine (JVM). They are not usually recoverable.  &lt;br&gt;
&lt;strong&gt;Exceptions&lt;/strong&gt; occur when something unexpected happens in your application during runtime. Exceptions can be recovered from.&lt;/p&gt;

&lt;p&gt;Padding the nutshell out a bit: &lt;/p&gt;
&lt;h2&gt;
  
  
  Errors
&lt;/h2&gt;

&lt;p&gt;Errors prevent an application from completing its task, resulting in it terminating or needing to be restarted. &lt;/p&gt;

&lt;p&gt;An Error can be caused by external factors such as system failures or resource depletion.  &lt;/p&gt;

&lt;p&gt;There are many error types, a few of the the common ones are: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Error Type&lt;/th&gt;
&lt;th&gt;Example Errors&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Class Loading Errors&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ClassNotFoundException&lt;/code&gt;, &lt;code&gt;NoClassDefFoundException&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linking Errors&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;NoSuchMethodError&lt;/code&gt;, &lt;code&gt;IncompatibleClassError&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runtime Errors&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;NullPointerException&lt;/code&gt;, &lt;code&gt;ClassCastExcpetion&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;System Level Errors&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;OutOfMemoryError&lt;/code&gt;, &lt;code&gt;StackOverflowError&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compilation Errors&lt;/td&gt;
&lt;td&gt;Syntax Issues, Type Mismatches&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Exceptions
&lt;/h2&gt;

&lt;p&gt;Exceptions occur when something unexpected - but not entirely beyond imagination - has happened within the code.&lt;/p&gt;

&lt;p&gt;Exceptions interrupt the expected flow of a program, but (unlike Errors) they can be caught and dealt with (‘handled’). &lt;/p&gt;

&lt;p&gt;In handling exceptions, we have the opportunity to recover from them and keep our application running. It's up to the Developer to decide how we handle these Exceptions - a few common coding scenarios and possible handling ideas are listed below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Exception&lt;/th&gt;
&lt;th&gt;Handled by:&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;File not found&lt;/td&gt;
&lt;td&gt;Create default file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Invalid input&lt;/td&gt;
&lt;td&gt;Request another input from user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Network failure&lt;/td&gt;
&lt;td&gt;Retry connection&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Note that an uncaught exception will become a runtime Error - so we must make sure to catch them!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Runtime Exceptions&lt;/strong&gt; deserve an honourable mention. They describe exceptions that occur within the JVM (during runtime). Runtime exceptions are extremely common, so much so that the compiler allows them to go uncaught and unspecified.&lt;/p&gt;

&lt;p&gt;Some common types Runtime Exceptions that you might have already encountered:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NullPointerException&lt;/code&gt; - when you try to perform operations on a null value&lt;br&gt;
&lt;code&gt;IllegalArgumentException&lt;/code&gt; - when an illegal argument has been passed&lt;br&gt;
&lt;code&gt;IllegalStateException&lt;/code&gt; - if an object has state that’s invalid &lt;/p&gt;
&lt;h2&gt;
  
  
  Catching Exceptions
&lt;/h2&gt;

&lt;p&gt;In Kotlin, we can catch Exceptions by wrapping code in try-catch blocks. The exceptions we're looking to catch are not mistakes made by the engineer but plausible but plausible scenarios that shouldn’t happen, but &lt;em&gt;could&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Some example scenarios where we might want to catch an exception: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A user enters an invalid username/password combination&lt;/li&gt;
&lt;li&gt;A file that we are searching for is not found&lt;/li&gt;
&lt;li&gt;User attempts to retrieve data that &lt;em&gt;should&lt;/em&gt; exist in a database but doesn't&lt;/li&gt;
&lt;li&gt;A user inputs zero as a divisor
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To take this last example and put it into some code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;divider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="n"&gt;b&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;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;printStackTrace&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// print the full stack trace of the exception&lt;/span&gt;
        &lt;span class="mi"&gt;0&lt;/span&gt;            &lt;span class="c1"&gt;// return a safe, default value&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;
  
  
  Throwing Exceptions
&lt;/h2&gt;

&lt;p&gt;We don’t always have to catch an exception at the point of its creation - in fact, it is often better to let the exception bubble up through the call stack. We do this by &lt;em&gt;throwing&lt;/em&gt; our exception.&lt;/p&gt;

&lt;p&gt;Throwing an Exception initialises an object with dynamic storage duration called the Exception Object. It might help to think of this Exception Object as a hot potato. The first person to pull it from the oven isn't wearing gloves and the potato gets tossed along a line of people until it reaches someone who &lt;em&gt;is&lt;/em&gt; wearing gloves. &lt;/p&gt;

&lt;p&gt; A few reasons why throwing an Exception (rather than just catching in situ) is a good idea: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DRY PRINCIPLES&lt;/strong&gt;
Avoids multiple try catch blocks everywhere, resulting in cleaner code that is easier to read and test&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CONTEXT&lt;/strong&gt;
There may be multiple scenarios in which the code triggering the exception is called. Each of these might benefit from a different, custom, response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RETRIES&lt;/strong&gt; 
If you catch an exception too early, you loose the opportunity to retry the call from an API level&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's our divider function example again, this time, however, the Exception is thrown and caught further up the call stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&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="nf"&gt;divider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&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;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;printStackTrace&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
      &lt;span class="mi"&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;fun&lt;/span&gt; &lt;span class="nf"&gt;divider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&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;b&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&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="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Centralised Error Handling
&lt;/h2&gt;

&lt;p&gt;In larger applications, all Exceptions can be managed in a single location, ensuring consistent handling and improved maintainability. This often happens on the very outer edge of the application, within the HTTP handler and is known as Centralised Error Handling.&lt;/p&gt;

&lt;p&gt;Whether or not we choose to implement Centralised Error Handling in our application, a good rule of thumb when working with Exceptions is 'Throw early , catch late'.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Exceptions
&lt;/h2&gt;

&lt;p&gt;Clarity is King when it comes to coding. Custom Exceptions allow us to be really specific about what's going wrong in a given instance - making unexpected behaviours easier to understand. We can create Custom Exceptions by extending the Exception class (or one of its subclasses).&lt;/p&gt;

&lt;p&gt;When creating a Custom Exception we can choose whether or not to pass an Exception message string as a parameter.&lt;/p&gt;

&lt;p&gt;Using our divider function as an example again, here are some custom exception ideas: &lt;br&gt;
No message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DivisionException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ArithmeticException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                 
&lt;span class="c1"&gt;// remember we can create our Exception from any Exception subclasses&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; 
   &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;DivisionException&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;versus &lt;em&gt;with&lt;/em&gt; Message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DivisionExceptionWithMessage&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="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&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="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; 
   &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;DivisionExceptionWithMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nc"&gt;You&lt;/span&gt; &lt;span class="n"&gt;can&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt; &lt;span class="n"&gt;like&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="n"&gt;can&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;different&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;every&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;thrown&lt;/span&gt;&lt;span class="err"&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;Custom Exceptions can be particularly useful when we want to observe unexpected behaviour specific for business logic - where the standard exceptions available don't quite describe the problem as we would like. &lt;/p&gt;

&lt;p&gt;Our divider function, now using our custom exception (with message):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;divider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&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;b&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&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="nc"&gt;DivisionExceptionWithMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"you cannot divide by zero"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&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="nf"&gt;divider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&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;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DivisionExceptionWithMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;printStackTrace&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;When we run the main function above, we get the following response in the IDE console:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqy93gtswqsibf7btthca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqy93gtswqsibf7btthca.png" alt=" " width="490" height="155"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Stack Trace
&lt;/h2&gt;

&lt;p&gt;The above image is an example of a Stack Trace. The Stack Trace is a list of the method call hierarchy. It shows us what the problem is, where to find its origin and how it was called. &lt;/p&gt;

&lt;p&gt;To break down this Stack Trace: &lt;br&gt;
Line 1 - Exception type and message - nice and clear thanks to custom Exception.&lt;br&gt;
Line 2 -  Points to the divider function, where the Exception was thrown.&lt;br&gt;
Line 3 - Shows the exact line where the divider function was called. &lt;br&gt;
Line 4 - Indicates the entry point to the application and where the Stack Trace starts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Note that because we followed the 'Throw early, catch late' philosophy, we got a full stack trace and this helps us get the full picture of where and why our Exception was triggered.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Things to Consider
&lt;/h2&gt;

&lt;p&gt;🔑 &lt;strong&gt;Security&lt;/strong&gt;&lt;br&gt;
We must carefully consider the information we disclose in custom Exception messages. Overly revealing messages will leave your application vulnerable to hackers and cyber criminals. If in doubt, an exception with no message is preferred. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Application speed&lt;/strong&gt;&lt;br&gt;
Throwing exceptions can slow down your application - something to bear in mind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real world Exception handling&lt;/strong&gt;&lt;br&gt;
In the examples in this post, we handle our Exceptions by printing the full stack trace to the console and/or returning a safe, default value. Whilst this approach is fine for small applications and exercises, a better approach for real applications would be one/all of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log the message to somewhere like Datadog where we can monitor and review our application&lt;/li&gt;
&lt;li&gt;If the Exception occurred in a Front End application we could return an error response to the user, requiring them to submit another set of numbers&lt;/li&gt;
&lt;li&gt;If the Exception occurs in an API, the Exception could be returned as a Http Status code - eg 400 Bad Request&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Handy tools&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Result&lt;/strong&gt;&lt;br&gt;
Kotlin's &lt;code&gt;Result&lt;/code&gt; is an exception wrapper which is designed to catch exceptions as values. It returns either a successful outcome (Result.success(value)) or a failure (Result.failure(exception)). This allows us to handle operations that might throw exceptions without using try/catch.&lt;br&gt;
&lt;strong&gt;Result4k, Arrow&lt;/strong&gt;&lt;br&gt;
Using the Result4k or Arrow libraries, we can replace exceptions with expected named error values which allows us to bypass the need for try catch blocks. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checked / Unchecked Exceptions&lt;/strong&gt;&lt;br&gt;
If you have a Java background, you may have heard of checked and unchecked exceptions. In Kotlin, all exceptions are unchecked — which means it's up to us to decide what to handle 😎.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
