6 Replies
      Latest reply on Apr 4, 2017 11:28 PM by stcocoa
      stcocoa Level 1 Level 1 (0 points)

        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

        • Re: Try Catch issue..
          eskimo Apple Staff Apple Staff (11,825 points)

          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"

          • Re: Try Catch issue..
            Claude31 Level 8 Level 8 (6,515 points)

            You can write :

             

                      do {
                         let a = try someFunc()
                         A()
                         B()
                         C()
                      } catch {  
                         doNothing()
                      }
            
            • Re: Try Catch issue..
              igorland Level 1 Level 1 (0 points)

              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
              }