Swift - Error Handling Last Updated : 06 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Error handling is a response to the errors faced during the execution of a function. A function can throw an error when it encounters an error condition, catch it, and respond appropriately. In simple words, we can add an error handler to a function, to respond to it without exiting the code or the application. Here we will be discussing outputs retained when an error is thrown and not. The following is an example of a function canThrowAnError() with an error handler implemented outside the loop in Swift. Example 1: func canThrowAnError() throws { // This function may or may not throw an error } do { try canThrowAnError() // no error was thrown } catch { // an error was thrown } Remember throws is a keyword that indicates that the function can throw an error when called. Since it can throw an error, we need to add the try keyword in the beginning when the function is called. Errors are automatically propagated out of their current scopes unless handled by the catch clause do propagate the errors to one or more catch clauses. Real-world scenario Case 1: It will throw an error: If the function will throw an error if the seat belt is not fastened or if the fuel is low. Because startCar() can throw an error, the function call is wrapped in a try expression. When a function is wrapped into a do function, it will be propagated to the provided clauses if any errors are thrown. Case 2: It will not throw an error: If the function is called. If an error is thrown, and it matches the carError.noSeatBelt case, then the blinkSeatBeltSign() function will be called. If an error is thrown, and it matches the carError.lowFuel case, then the goToFuelStation(_:) function is called with the associated [String] value captured by the catch pattern. Let us now demonstrate the above case in the same programming language: Example 2: func startCar() throws { // Insert the body of the function here } do { try startCar() turnOnAC() } catch carError.noSeatBelt { blinkSeatBeltSign() } catch carError.lowFuel(let fuel) { goToFuelStation(fuel) } Create Quiz Comment A aashaypawar Follow 0 Improve A aashaypawar Follow 0 Improve Article Tags : Swift Explore Swift IntroductionSwift - Hello World Program2 min readSwift - Basic Syntax5 min readSwift - Keywords4 min readSwift - Literals10 min readSwift - Constants, Variables & Print function2 min readSwift - Operators8 min readSwift Data TypesSwift - Data Types5 min readSwift - Integer, Floating-Point Numbers2 min readStrings in Swift8 min readString Functions and Operators in Swift10 min readHow to Concatenate Strings in Swift?3 min readHow to Append a String to Another String in Swift?2 min readHow to Insert a Character in String at Specific Index in Swift?2 min readHow to check if a string contains another string in Swift?2 min readHow to remove a specific character from a string in Swift?2 min readData Type Conversions in Swift5 min readSwift - Convert String to Int Swift3 min readSwift Control FlowSwift - Decision Making Statements5 min readSwift - If Statement3 min readSwift - If-else Statement3 min readSwift - If-else-if Statement3 min readSwift - Nested if-else Statement4 min readSwift - Switch Statement10 min readSwift - Loops5 min readSwift - While Loop2 min readSwift - Repeat While loop3 min readSwift - For-in Loop6 min readSwift - Control Statements in Loops5 min readSwift - Break Statement6 min readSwift - Fallthrough Statement4 min readSwift - Guard Statement15 min readSwift FunctionsCalling Functions in Swift6 min readSwift Function Parameters and Return Values10 min readHow to Use Variadic Parameters in Swift?6 min readSwift - In Out Parameters4 min readSwift - Nested Function5 min readSwift - Function Overloading8 min readClosures in Swift9 min readEscaping and Non-Escaping Closures in Swift5 min readHigher-Order Functions in Swift10 min readSwift CollectionsSwift - Arrays9 min readSwift - Arrays Properties4 min readSwift - How to Store Values in Arrays?4 min readHow to Remove the last element from an Array in Swift?4 min readHow to Remove the First element from an Array in Swift?4 min readHow to count the elements of an Array in Swift?2 min readHow to Reverse an Array in Swift?3 min readSwift Array joined() function3 min readHow to check if the array contains a given element in Swift?3 min readSorting an Array in Swift5 min readHow to Swap Array items in Swift?2 min readHow to check if an array is empty in Swift?2 min readSwift - Sets15+ min readSwift - Set Operations9 min readHow to remove first element from the Set in Swift?2 min readHow to remove all the elements from the Set in Swift?2 min readHow to check if the set contains a given element in Swift?3 min readHow to count the elements of a Set in Swift?2 min readSorting a Set in Swift4 min readHow to check if a set is empty in Swift?2 min readHow to shuffle the elements of a set in Swift?2 min readSwift - Difference Between Sets and Arrays3 min readSwift - Dictionary9 min readSwift - Tuples3 min readSwift - Iterate Arrays and Dictionaries6 min readSwift OOPsSwift Structures7 min readSwift - Properties and its Different Types13 min readSwift - Methods5 min readSwift - Difference Between Function and Method4 min readSwift - Deinitialization and How its Works?4 min readTypecasting in Swift7 min readRepeating Timers in Swift4 min readNon Repeating Timers in Swift7 min readDifference between Repeating and Non-Repeating timers in Swift4 min readOptional Chaining in Swift9 min readSingleton Class in Swift4 min readSwift Additional TopicsSwift - Error Handling2 min readDifference between Try, Try?, and Try! in Swift4 min readSwift - Typealias7 min readImportant Points to Know About Java and Swift3 min readDifference between Swift Structures and C Structure4 min readHow to Build and Publish SCADE Apps to Apple and Google Stores?11 min read6 Best iOS Project Ideas For Beginners8 min read Like