2

I do not know how do I handle runtime error in swift. I need to check for a parsing error and do something about it. Can anyone help me please?

I have code something like this:

var:SomeObject = parse("some string")

I need to handle any generic error that occurs in runtime. Thanks!

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
russell
  • 165
  • 2
  • 12
  • It really depends what's happening in parse function. What are you using it for? – Shamas S Apr 01 '15 at 08:34
  • I am parsing to to generate some mathematical expression. I needed to handle that error generically, it is too complicated to validate all the possibilities where error may occur. – russell Apr 01 '15 at 08:36
  • See at this post: http://stackoverflow.com/questions/24023112/try-catch-exceptions-in-swift – dimpiax Apr 01 '15 at 08:40
  • The parse function is yours, right? The calculations in it are being done by you? or it is a code that you got from somewhere? – Shamas S Apr 01 '15 at 08:41
  • @russell posted an answer below, I think you might require some assistance to setup your parse function properly as well. :) – Shamas S Apr 01 '15 at 08:50
  • It doesn't seem like a good idea to actually have arbitrary runtime errors be *allowed* in your code! You should really distinguish parsing errors from other things (which could be bugs, after all). – Andrew Jaffe Apr 01 '15 at 08:57
  • Thanks for posting answer. The parse function contains classes.. methods. I am not sure how everything will run smoothly and safely return nil when error occurs. But I understand you are implying to validate every bit of code so that it returns nil case of error...? Which is what I needed avoid due to nature of its complicity as I do not plan to do anything with this validation. – russell Apr 01 '15 at 09:03

2 Answers2

0

If the function is yours, then in case of a failure, you can make that function return a nil value.

That way your line of code

var anObj:SomeObject = parse("some string")

would become something like this

var:SomeObject? = parse("some string")

Notice the ? sign. It means that the value in it is Optional. In simple words, it could be some actual value, or it could be nil. After this function, you should perform a check like

If anObj != nil
{
//do something
}
else
{
//the parse didn't go right, handle the erorr here.
}
Shamas S
  • 7,507
  • 10
  • 46
  • 58
0

Swift 2 adds additional safety to your error checking. You use the throws keyword to specify which functions and methods could throw an error. Then you have the do, try, and catch keywords for when you call something that could throw:

// 1
enum ParseError: ErrorType {
  case InvalidValue
}

// 2
func parseWithError(value:String) throws {
  if value.count > 0 {
    // yeaaa!
  } else {
    // 3
    throw ParseError.InvalidValue
  }
}

func parse(value:String) {
  // 4
  do {
    try parseWithError(value)
  } catch {
    print("Could not parse! :[")
    return
  }
}

There are a few things to highlight here:

  1. To create an error to throw, simply create an enum that derives from ErrorType.
  2. You need to use the throws keyword to mark any function that can throw an error.
  3. This throws an error, which will be caught in section 4.
  4. Instead of try blocks, which might be familiar from other languages, you wrap any code that can throw an error in a do block. Then, you add the try keyword to each function call that could throw an error.

For more read here or this is the official documentation of Error Handling

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97