Swift basics - error handling
Error handling
Guard statement
If a condition if false, we can trap errors early:
var x = 3
guard x > 10 else
{
//do error handling here
}
If x is not greater than 10, error handling will be done.
In a function, check that there is a valid value in an optional:
func theFunction(str: String?)
{
guard let theStr = str else
{
//error handling
}
}
Control statements
You use control statements for error handling.
Control statements include continue, break, fallthrough, guard, throws and return
Continue:
tells a loop to stop executing the code and go to the next iteration in the loop
for i in 1 ... 10
{
if i % 2 == 0
{
continue
}
print("Odd number")
}
Break:
If a criteria is met, it will get out of the loop. The for loop will not continue:
for i in 1... 10
{
if i % 2 == 0
{
break
}
print("odd number")
}
The above will only print odd number once.
Fallthrough:
Used in switch statements
switch food
{ case "Laksa" : print("Laksa")
fallthrough
case "Kway Teow": print("Kway Teow")
fallthrough
default: print("Other food")}
In the above case, if the food is laksa, all three will be printed.
You can create an enumeration for handling different types of errors:
enum KeyErrors: Error
{
case LessThanZero(description: String)
case NotANumber(description: String)
case AlreadyExists
}
Next, you can create a type alias of a Customer, which is actually a tuple, just that you can refer to it using the name Customer:
typealias Customer = (firstName: String, lastName: String, number: Int)
In your CustomerBase structure, you can throw an error at the methods:
THROW ERROR
mutating func addCustomer(person: Customer) throws{
guard customer.customer >= 0 else
{ throw KeyErrors.LessThanZero(description: "Number is less than zero")}
}
CATCH error:
do{
try newCustomer.addCustomer(customer1)
} catch KeyErrors.LessThanZero(let description){print(description)}
} catch KeyErrors.NotANumber(let description){print(description)}
OR
if let iceCream = try? food.getIceCream(flavour: 3) {}
If we use a defer block, it will still run even if an error is thrown:
do{
try newCustomer.addCustomer(customer1)
} catch KeyErrors.LessThanZero(let description){print(description)}
} catch KeyErrors.NotANumber(let description){print(description)}
Guard statement
If a condition if false, we can trap errors early:
var x = 3
guard x > 10 else
{
//do error handling here
}
If x is not greater than 10, error handling will be done.
In a function, check that there is a valid value in an optional:
func theFunction(str: String?)
{
guard let theStr = str else
{
//error handling
}
}
Control statements
You use control statements for error handling.
Control statements include continue, break, fallthrough, guard, throws and return
Continue:
tells a loop to stop executing the code and go to the next iteration in the loop
for i in 1 ... 10
{
if i % 2 == 0
{
continue
}
print("Odd number")
}
Break:
If a criteria is met, it will get out of the loop. The for loop will not continue:
for i in 1... 10
{
if i % 2 == 0
{
break
}
print("odd number")
}
The above will only print odd number once.
Fallthrough:
Used in switch statements
switch food
{ case "Laksa" : print("Laksa")
fallthrough
case "Kway Teow": print("Kway Teow")
fallthrough
default: print("Other food")}
In the above case, if the food is laksa, all three will be printed.
You can create an enumeration for handling different types of errors:
enum KeyErrors: Error
{
case LessThanZero(description: String)
case NotANumber(description: String)
case AlreadyExists
}
Next, you can create a type alias of a Customer, which is actually a tuple, just that you can refer to it using the name Customer:
typealias Customer = (firstName: String, lastName: String, number: Int)
In your CustomerBase structure, you can throw an error at the methods:
THROW ERROR
mutating func addCustomer(person: Customer) throws{
guard customer.customer >= 0 else
{ throw KeyErrors.LessThanZero(description: "Number is less than zero")}
}
CATCH error:
do{
try newCustomer.addCustomer(customer1)
} catch KeyErrors.LessThanZero(let description){print(description)}
} catch KeyErrors.NotANumber(let description){print(description)}
OR
if let iceCream = try? food.getIceCream(flavour: 3) {}
If we use a defer block, it will still run even if an error is thrown:
do{
try newCustomer.addCustomer(customer1)
} catch KeyErrors.LessThanZero(let description){print(description)}
} catch KeyErrors.NotANumber(let description){print(description)}
defer{print("This will still be printed"}
Comments
Post a Comment