3

I come from a .NET background, where error handling can be as simple as wrapping a set of statements in a try-catch. For example:

try
{
   statement1
   statement2
}
catch (ex)
{
   log(ex.Message)
}

I'm trying to add error handling in my Swift project, and all the articles I have read so far seem to indicate that error handling requires more work in Swift. In the example above, it seems I would need to know exactly which statement throws an error, and then add a "try" before it. Is it not possible to simply wrap a block of code in a try-catch, and inspect the error that gets thrown?

8
  • Yes, that is correct. Commented Dec 11, 2016 at 18:06
  • 1
    Wow, that seems like a lot of work. My app is crashing, and all I wanted to do is add a simple try/catch around the suspected code and do some logging, but it seems like I need to do a lot of exception design work before I can do that! Commented Dec 11, 2016 at 18:13
  • 1
    To inspect the error that gets thrown, set an exception breakpoint. Commented Dec 11, 2016 at 18:17
  • 1
    Is it really that unreasonable to require devs to know what functions they're trying to catch errors from? Commented Dec 11, 2016 at 19:28
  • 1
    @AlexanderMomchliov Sometimes, in service oriented architecture, there is so much dependency on 3rd party software, and we can't always design for all error possibilities up front--it's an incremental process. Having the ability to quickly detect an arbitrary exception can be a great boon for developers. C# does this really well. Swift chose not to give us this power, possibly for good reasons. But that doesn't mean that it's not a good feature to have in a programming language. Commented Dec 11, 2016 at 19:50

2 Answers 2

3

Nope, you can't just wrap a block of code with try-catch.

First of all, cause not every line of code could produce exceptions. Only functions marked as "throws" could produce exceptions.

For example, you have some function:

deserialise(json: JSON) -> MyObjectType throws

And let this functions throws exceptions of type MyErrorType

That's how you should use it:

....
do {
  let deserialisedObject = try deserialise(jsonObject)
  ... // do whatever you want with deserialised object here
} catch let error as MyErrorType {
  ... // do whatever you want with error here
}
...

So, as you see, exceptions in swift is not the same thing as exceptions in C++(or other regular language) for .NET

Sign up to request clarification or add additional context in comments.

Comments

-1

You can use try catch in swift just like you do in .net with this little change in structure, before that create a enum of exceptions which you want to catch like follows

//exceptions to catch, change as per your need
enum someActionError: Error {
    case invalidSelection
    case outOfStock
}

//use
do {
    try statement1
} catch someActionError.invalidSelection {
    print("Invalid Selection.")
} catch someActionError.outOfStock {
    print("Out of Stock.")
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.