<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by SurajPrasad on Medium]]></title>
        <description><![CDATA[Stories by SurajPrasad on Medium]]></description>
        <link>https://medium.com/@surajprasad2099?source=rss-9657d7da169f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*KIDSYlqlJEFaFRPgA38URA.jpeg</url>
            <title>Stories by SurajPrasad on Medium</title>
            <link>https://medium.com/@surajprasad2099?source=rss-9657d7da169f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:00:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@surajprasad2099/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Error Handling in Swift]]></title>
            <link>https://medium.com/@surajprasad2099/error-handling-in-swift-8da37558e853?source=rss-9657d7da169f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8da37558e853</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[error-handling]]></category>
            <dc:creator><![CDATA[SurajPrasad]]></dc:creator>
            <pubDate>Thu, 09 Mar 2023 06:06:56 GMT</pubDate>
            <atom:updated>2023-03-09T06:07:43.608Z</atom:updated>
            <content:encoded><![CDATA[<p>Error handling is the process of responding to and recovering from error conditions in your program. Swift provides first-class support for throwing and catching.</p><p><strong>Let’s start with example</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O5z3xtz-yGni76DC7SSDsg.png" /></figure><p>Here, we are trying to divide a number by zero. So this type of error causes the abnormal termination of the program, as seen in the above screenshot of the playground.</p><p><strong><em>Steps For Error Handling in Swift</em></strong></p><ol><li>Create an enum that represents the types of errors.</li><li>Create a throwing function using the throws keyword.</li><li>Call the function using the try keyword.</li><li>Wrap the code with try in the do {…} block and add the catch {…} block to handle all errors.</li></ol><p><strong>Create an enum of Errors</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*hSyoXSETvY2Y4bsBEBELsQ.png" /></figure><p><strong>Creating a throwing method using the throws keyword</strong></p><p>Creating a throwing method is as easy as adding the <strong>throws </strong>keyword to a method just before the return statement.</p><p>In this example, we use a <strong>division(numerator:Int, denominator: Int)</strong> function throws error based on the value passed during the function call, the function throws an error if the error condition is met(dividedByZero value of the DivisionError enum if denominator == 0).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UEDP25faj1bHlJlr4Dtf8Q.png" /></figure><p>When calling a throwing method you might get the following error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LwqyGJVSH63_Mal9cTCDQg.png" /><figcaption>Calling a throwing method without the try keyword</figcaption></figure><p>This means that you have to use the try keyword before a piece of code that can throw an error.</p><p><strong>Method call using try keyword</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PdRRmL2i5kwVuhvhXhAvoQ.png" /><figcaption>Calling a throwing method with the try keyword</figcaption></figure><p>we use the <strong>try</strong> keyword while calling the throwing function. It indicates that a function can throw an error.</p><p>However, the error handling process is still not complete. If we run the program now, we’ll get an error message.</p><p><strong>Playground execution terminated: An error was thrown and was not caught:</strong></p><p><strong>__lldb_expr_151.DivisionError.dividedByZero</strong></p><p><strong>Handling Errors in Swift with a do-catch statement</strong></p><p>To catch a thrown error in Swift we need to use the do-catch statement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*fqynOOpEPaJKE6TAlsgT1A.png" /><figcaption><strong>Handling Errors in Swift with a do-catch statement</strong></figcaption></figure><p>Here, we’ve called the throwing <strong>division(numerator:Int, denominator: Int) </strong>method from within the <strong>do </strong>block and the <strong>catch </strong>block to catch the error in case the function throws one(executed based on the enum value of DivisionError)</p><p>e.g. <strong>Handling Errors in Swift</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*88DkWtJwlraKWytnewrpCA.png" /><figcaption><strong>Handling Errors in Swift</strong></figcaption></figure><p>I believe you now have a better understanding of how errors are handled in Swift.</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8da37558e853" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>