Error Handling
Error Handling
Error Handling is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 1. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 1 lessons in total.
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
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.

Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Error Handling” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Error Handling”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 1 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 1.
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.