Questions tagged [do-catch]
29 questions
22
votes
6 answers
Swift Error Handling For Methods That Do Not Throw
How to handle errors for methods or code that does not explicitly throw?
Wrapping it a do / catch block results in a compiler warning:
"'catch' block is unreachable because no errors are thrown in 'do' block"
Coming from C# / JAVA background this…

AlexVPerl
- 7,652
- 8
- 51
- 83
14
votes
3 answers
Nested do catch swift 3.0
I'd like to use consecutive try statements. If one returns an error I'd like to proceed to the next one, otherwise return the value.
The code below seems to work fine, however I'll end up with a big nested do catch pyramid. Is there a…

Mymodo
- 163
- 1
- 7
9
votes
0 answers
Error handling for URLSession.shared.datatask
I have a function that queries an API and then populates an array of objects based on the results(see code below). From the JSON retrieved I can use guard statements to catch any error meaning that if data is ever missing it is captured however if…

Conor
- 736
- 1
- 13
- 33
5
votes
2 answers
NSError and Error in Swift
I've seen that there is already a question on the difference between NSError and Error in Swift and I am aware of the differences. However, I do not understand the behavior of the code snippet below, since it compiles correctly. Swift's error…

Fernando Castor
- 51
- 1
- 6
4
votes
2 answers
Returning String from a do-catch statement
I was trying to translate the code from swift 2 to swift 4 and came across this error
Errors thrown from here are not handled
So I did this but now it tells me to return a string. Any idea how to do this?
func formatSentence(sentence:String)…
user9137935
4
votes
1 answer
How to avoid nesting do/catch statements in Swift2
I keep wanting to do this:
do {
let result = try getAThing()
} catch {
//error
}
do {
let anotherResult = try getAnotherThing(result) //Error - result out of scope
} catch {
//error
}
But seem only to be able to do this:
do {
…

nwales
- 3,521
- 2
- 25
- 47
2
votes
1 answer
How to get the correct error when hitting a do catch block for a function that throws when String does not conform to Error?
import Foundation
enum ErrorScenarios: Error {
case invalidAge
case invalidEmail
case incorrectData
}
func age(age:Int) throws {
if age < 20 {
throw ErrorScenarios.invalidAge
}
print("You have a valid age of…

anonymousSwift
- 99
- 1
- 6
2
votes
0 answers
Where to write try statment?
What are the differences between writing try before variable or after it assignment sign? I mean this:
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundUrl!)
} catch {
print(error)
}
and this:
do {
try audioPlayer =…

dziobaczy
- 891
- 9
- 17
2
votes
2 answers
Extra argument 'error' in call
I am getting this error Extra argument 'error' in call
Code in Context
var post:NSString = "name=\(Username)&email=\(Email)&phone=\(phonenumb)&password=\(Password)&address=\(address)"
NSLog("PostData: %@",post);
var url:NSURL =…

mack
- 603
- 2
- 9
- 22
1
vote
2 answers
How can you also get the error **instance** when do/catching specific error types?
I love when simple things you think will take all of two minutes ends up taking over an hour!
When writing unit tests, we need to test for several specific error types, some of which have associated values. We also need access to the error itself…

Mark A. Donohoe
- 28,442
- 25
- 137
- 286
1
vote
2 answers
Swift How to returning a tuple from a do catch where conditional binding must have optional type?
I am wanting to put a swift 3 do-catch inside a function rather than constantly writing it everywhere I need it; inside this function I wish to return a tuple with a boolean, and an optional error.
I am trying to return a tuple from the function and…

zardon
- 1,601
- 4
- 23
- 46
1
vote
3 answers
When to use do-catch block using Swift
In the following scenario when reading JSON data from a file, I have the following block of code:
// Fetch URL
let url = Bundle.main.url(forResource: "sampleJSON", withExtension: "json")!
// Load Data
let data = try! Data(contentsOf: url)
//…

syedfa
- 2,801
- 1
- 41
- 74
1
vote
1 answer
The do statement
Let's say this is our function for delete objects from the model:
func delete(indexPath: IndexPath) {
let managedObject = self.fetchedResultsController.object(at: indexPath)
self.managedObjectContext.delete(managedObject)
do {
…

Mannopson
- 2,634
- 1
- 16
- 32
1
vote
1 answer
Do-Catch and overflow
How I can wrap in do catch overflow error
let increasePerSecond: UInt32 = UInt32.max
let offset: UInt32
do {
offset = try ((nowTimeInterval - calculatedTimeInterval) * increasePerInterval)
} catch _ {
offset = 0
…

EvGeniy Ilyin
- 1,817
- 1
- 21
- 38
1
vote
2 answers
Getting Variable out of do-catch statement in swift
I want to know how to use a variable which is staying in a do-catch statement. I'm parsing some JSON from the web and filling an object with it but then I need that object outside to fill a UITableView. The function where I get web info:
func…

manhols
- 25
- 4