Try Catch issue..

Hi ,


I want to try codes (many rows!) and if there is exception (I mean anything for app fall down happend) , I want to ignore it. (just like nothing happen)

Is this possible to do by using do-try-catch ?


I tried that, but, it work with one row programming, but not to many rows.


I mean , Try A is OK , but

Try {

A;

B;

C;

}

is not wotking.


Because I can not use { } after Try.


Then , any way to do this, in Swift3 ??


Thanks🙂

Replies

Swift’s error mechanism is documented in the Error Handling section of The Swift Programming Language. I recommend you start by reading through that and then get back to us if you have follow-up questions.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

You can write :


          do {
             let a = try someFunc()
             A()
             B()
             C()
          } catch {  
             doNothing()
          }

You may also do some custom errors. Sonething like this...


Create your custom errors.


enum CustomError : Error
{
    case error1
    case error2
}
extension CustomError : CustomStringConvertible
{
    public var description : String
    {
        switch self
        {
        case .error1:
            return "ERROR1: This is error 1"
        case .error2:
            return "ERROR2: This is error 2"
        }
    }

    public var errorNumber : String
    {
        switch self
        {
        case .error1:
            return "1"
        case .error2:
            return "2"
        }
    }
}


Then...


do
{
   let a = A()
   if a == 0
   {
      throw CustomError.error1
   }

   let b = B()
   if b == 0
   {
      throw CustomError.error2
   }
}

catch let error as CustomError
{
      let errorMsg = error.description + error.errorNumber
}

I mean, I want to do try group of things.


like .. ->


try  {  
A( )
B( )
C( )
}


Well, I may create function including A(), B(), C() , and try that function.


Thanks!

Thank you for introducing me that!

actually I had read that document, but like a fool , I couldnt find answer..

Thanks for telling me about custom error handling !