Questions tagged [do-catch]

26 questions
20
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
  • 6,653
  • 5
  • 36
  • 74
10
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
  • 123
  • 6
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
  • 600
  • 1
  • 9
  • 29
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,301
  • 2
  • 20
  • 42
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…
2
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…
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 =…
OfftheCode
  • 306
  • 2
  • 9
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
  • 503
  • 1
  • 7
  • 20
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,425
  • 2
  • 17
  • 40
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,707
  • 1
  • 34
  • 71
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,426
  • 1
  • 13
  • 29
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,459
  • 1
  • 18
  • 29
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
  • 3
1
vote
2 answers

do try catch swift 2

HI i am a little confused about how to transfer if_else error handling to do try catch successfully. Here is my code. let error : NSError? if(managedObjectContext!.save()) { …
J.Chang
  • 35
  • 5
1
2