Error Handling
Error Handling
Error Handling is a free Swift Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Introduction
Hello,
While the application is running, some errors may occur depending on the code of our application (logic errors, etc.) or depending on the environment in which the application is running.
Error handling can be used during the identification and control of errors that occur.
error types
After an error occurs, error handling can prevent the flow of the application from disrupting, log errors or, in some cases, close the application (in cases where it is impossible to run).
Some of the errors that can occur can be caused by logic errors in the code of the software developers. For example, the application will throw an error when we force unwrap an optional value that we learned in the previous section.
In some cases, errors may occur independent of the developer. For example, if there is no disk space on the device where the application is running, an error will occur in our application again.
custom error enum
We can define custom errors that will occur with an enum ourselves. In the example below, the error types that may occur when entering our credit card information are added with the name CreditCardError.
enum CreditCardError: Error {
case insufficientFunds
case issuerDeclined
case invalidCVC
}throws keyword
If a method we wrote can generate an error, a throws keyword is added to the method's definition.
Below is an example of the syntax.
func processPayment(creditcard: String) throws {
}throw keyword
Sometimes, while writing our code, the software developer may need to throw the error.
Let's imagine. We write a payment system in a bank.
And the customer's balance is not enough when paying by credit card. In this case, we can throw an error that the fund is not sufficient.
throw CreditCardError.insufficientFundsdo-catch block
do-catch block
When we want to use methods that may cause errors in our application, we can capture possible errors by taking these codes into do-catch blocks.
While the codes that may cause errors are located in the do block, possible errors will be captured in the error catch block.
Let's look at the example below
do {
//Code block that may throw an Error
}
catch let error {
print(error)
}try syntax
Except for the try keyword. We have try! or try? usages.
Let's look at our example.
// if an error was thrown, app will crash
let result = try! doSomething()
if let result = try? make() {
//If the make method is successful, the result is unwrap. It is a very elegant use.
} else {
//make() threw an error.
}multiple catch block
If the method we call can throw multiple errors, we can filter and direct our application flow according to the type of error.
In the example below, each catch block handles different errors that may occur.
do {
try doSomething()
} catch CustomError.errorOne {
//handle errorOne Error
} catch CustomError.errorTwo(let customErr) {
//handle errorTwo Error with customErr
}Congratulations
Congratulations 🎉
In this lesson, we learned how to handle application errors with Swift.
See you in the next lesson.

Frequently asked questions
Is the “Error Handling” lesson free?
Yes — the full text of “Error Handling” is free to read here on the web, and the Swift Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Error Handling”?
Error Handling You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.
How long does the “Error Handling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.