Difference between revisions of "Error Handling"

From Coder Merlin
(Created page with "Error Handling The process of responding to and recovering from errors in your software is called error handling. At runtime, Swift provides first-class support for throwing,...")
 
Line 1: Line 1:
Error Handling
Error Handling in Swift
The process of responding to and recovering from errors in your software is called error handling. At runtime, Swift provides first-class support for throwing, catching, propagating, and transforming recoverable exceptions.
The process of responding to and recovering from errors in your software is called error handling. At runtime, Swift provides first-class support for throwing, catching, propagating, and transforming recoverable exceptions.



Revision as of 00:39, 6 October 2021

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Error Handling in Swift The process of responding to and recovering from errors in your software is called error handling. At runtime, Swift provides first-class support for throwing, catching, propagating, and transforming recoverable exceptions.

There are times when successful execution or a beneficial output is not assured. If there isn't any data to return, optionals are used to indicate that fact; however, when an operation fails, it's often useful to understand what went wrong so your code may react appropriately.

Consider the challenge of reading and processing data from a file on disk, for example. There are several ways this operation might fail, including the file not existing at the correct path, the file not having read permissions, or the file being encoded in an incompatible format. Differentiating among these different situations allows a program to address some issues and notify users about any they can't resolve.

Things to Learn In this tutorial, you will learn the following concepts:

· How to define custom errors that can be thrown, caught, and handled.

· The difference between throws and propagating errors.

· What the try, throw, catch, and do keywords are for.

Before diving into Swift's error handling model in detail (the focus of this tutorial), it helps to understand what an error is at a more abstract level. This article discusses how to model recoverable errors as values—specifically as instances of Swift's ErrorType enum type—and provides an introduction to throwing and catching errors using both synchronous and asynchronous APIs, then concludes with some guidance on using convention over configuration when writing your own error types.

Throwing Errors In Swift, error values are represented by types that conform to the Error protocol. This empty protocol implies that a type may be used for error handling.

Enumerations in Apple's Swift programming language are well suited to modeling a set of associated error conditions, with values that may be used to provide more information about the nature of an issue.

    • CODE SNIPPET HERE**

An error is a runtime condition that prevents the normal flow of execution from continuing. To throw an error, you use the throw statement.

    • CODE SNIPPET HERE**

Handling Errors When an error is raised, surrounding code must handle it—for example, by fixing the problem, exploring a different path, or informing the user of the failure.

In Swift, there are four different ways to deal with faults. You may propagating the issue from a function to the code that calls it, utilizing a do-catch construct, taking care of it as an optional value, or making sure that it does not happen again.

When a function fails, it affects the course of your program, so finding locations in your code that might produce problems fast is critical. Use the try? or try! variants of the try keyword to find these locations in your code before a piece of code that may call an error-throwing procedure, method, or initializer. The following paragraphs describe these keywords in further detail.

Throwing Functions After the parameters, you put the throws statement in a function's declaration to indicate that it may throw an error. A throwing function is one that has the throws keyword appended to its name. You write the throws keyword before any return arrow ( ->) if the function returns a value.

    • Code Snippet**

When code inside of a throwing function is executed, it propagates any problems that are thrown to the scope from which the call was made.

    • Code Snippet**

The withdraw(amount:) method is implemented using guard statements to exit the procedure early and throw appropriate errors if any of the conditions for purchasing a snack aren't met. Because a throw statement immediately transfers program control, an item will be vended only if all of these criteria are satisfied.

Do-Catch Blocks You run a block of code in a do-catch statement if an error is detected. When the code in the do clause throws an exception, it's checked against the catch clauses to see which one can handle it. The following is the general form of a do-catch statement:

    • Code Snippet**

You write a pattern after catch to specify the errors that clause can handle. If a catch clause does not include a pattern, any exception will be bound to error and interpreted as a local constant named error. For more information on pattern matching, see Patterns.

    • Code Snippet*

The catch clauses are used to decide whether to allow propagation of an error. Because the function can throw an error, it is called in a try expression. If an error occurs, control immediately transfers to the catch clauses, which must determine whether the error should be propagated. If no pattern is matched, the exception is caught by the last catch clause and assigned to a local error constant. The remainder of the do statement's code is run if no error occurred.

The catch clauses don't have to deal with the do clause's numerous possibilities for error. If none of the catch clauses can handle the situation, it will be passed on to the surrounding scope. On the other hand, the generated problem must be addressed by some surrounding scope.

In a nonthrowing function, the mistake must be handled by an enclosing do-catch block. Throwing functions require either an enclosing do-catch statement or may be dealt with by another part of your program (typically referred to as a "caller"). If the error is not handled at the top level, you will receive a runtime error.

Converting Errors to Optional Values You convert a problem to an optional value by using the try? operator. If the try? expression evaluates to nil, so does the value of the statement. In this case, for example, x and y have the same value and behavior:

    • Code Snippet**

An exception arises if someThrowingFunction() throws an error, in which case x and y are both set to nil. Otherwise, the value of x and y is the same as that of the function's return. Note that since whatever type someThrowingFunction() delivers is optional, so too are x and y. Because the function here returns an integer, therefore x and y are integers.

The try? syntax allows you to write brief error handling code when you want to handle all errors the same way. The following snippet, for example, uses several techniques to get data or returns nil if every attempt fails.

    • Code Snippet**

Disabling Error Propagation Sometimes, when a throwing operation or approach is invoked, you know that it will not cause an exception at runtime. On those occasions, you may use try! before the expression to prevent error propagation and wrap the call in a runtime assertion that no error will be thrown. If an error is generated by the code, you'll get a runtime exception.

For example, the following code uses a loadImage(atPath:) function, which loads the image resource at a specified path or throws an error if the image can't be loaded. Because the picture is already included in the application, no error will be thrown at runtime, so it's safe to disable error propagation.