Error Handling Flashcards

1
Q

What is error handling?

A

Error handling is the process of responding to and recovering from error conditions in your program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an error?

A

Code that produces an incorrect or unexpected result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 4 ways to handle errors in swift?

A

Propagating the errors using throwing functions.
Handling errors using do-catch
Converting errors to optional values
Disabling Error Propagation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you propagate errors using throwing functions(show an example)?

A
You write the throws keyword in the function’s declaration after its parameters 
Func canThrowErrors() throws -> String 
Also need the word throw when selecting what error to throw
(example)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you test a throwing function?

A

You have to use the try keyword

example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you handle errors using do-catch?

A

If an error is thrown by the code in the do clause, it passes it to catch clause to see what errors the clause can handle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if the catch clause doesn’t have a pattern?

A

The clause matches any error and binds the error to a local constant named error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you convert errors to optional values?

A

You use try? to handle an error by converting it to an optional value
If an error is thrown while evaluating the try? expression, the value of the expression is nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In what order are defer statements executed?

A

Reverse order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What language construct allows us to easily deal with domain errors in Swift?

A

Optionals

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

An error that occurs because a result is outside the accepted range of values is known as a

A

Domain error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Errors that are raised before your code is run is commonly known as a:

A

Compiler error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Where do we add the throws keyword to indicate that the function can throw an error?

A

After the parameter list and before the return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

TF: The throw keyword is a control transfer statement and exits the current scope

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

To allow an object to model an error, we conform to the ___________ protocol

A

Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

TF: An error is said to occur when you get an incorrect or unexpected result

A

True

17
Q

TF: By default all functions throw errors

A

False

18
Q

To call a throwing function we need to mark it with a certain keyword to indicate that this function can return an error. What is the keyword?

A

Try

19
Q

TF: A catch clauses cannot pattern match on different error cases like a switch statement

A

False

20
Q

Where are errors thrown from a function handled?

A

Catch clause

21
Q

The call to a throwing function along with the happy path of code is encapsulated in a _______ clause.

A

Do

22
Q

To execute statements as we leave the current scope we use a ________ statement

A

Defer

23
Q

In Swift, you can indicate what kind of error is being thrown from a function, i.e, the function preserves type information of thrown errors.

A

False, We can only indicate an error is thrown, not the type of error

24
Q

When an error is thrown from inside a do clause, what happens?

A

The error is propagated to its outer scope and handled by a catch clause.