12 Replies
      Latest reply: Jan 24, 2017 12:06 AM by QuinceyMorris RSS
      stcocoa Level 1 Level 1 (0 points)

        Hi ,

         

        Is there any way to get callback when there is NO error by do-catch in swift ?

         

        Because I think if it is possible, I can use it as callback functon.

         

        Thanks ^^

        • Re: do - catch
          QuinceyMorris Level 6 Level 6 (2,465 points)

          Do you mean something like a [hypothetical] do-catch-else construct? If so, you can already do that by putting "else" part inside but at the end of the "do" block. For example, instead of something like this:

           

               do {
                    A ()
               }
               catch {
                    B ()
               }
               elseifnocatch {
                    C ()
               }
          

           

          you can just do this:

           

               do {
                    A ()
                    C ()
               }
               catch {
                    B ()
               }
          

           

          Or have I misunderstood what you meant?

            • Re: do - catch
              stcocoa Level 1 Level 1 (0 points)

              Hi, QuinceyMorris

               

              Thanks,

               

              and How do I code elseifnocatch part ?

               

              1. do {
              2.           A ()
              3.      }
              4.      catch {
              5.           B ()
              6.      }
              7.      else {// Consecutive statements on a line must be separated by ';'        compile ERROR
              8.      C ()
              9.      }

               

              this does not work..

            • Re: do - catch
              KMT Level 8 Level 8 (6,935 points)

              Does that mean you want to get a callback on a non-error?

               

              Callback to do what?

               

              Perhaps if you gave an example of the broader code involved and what it is you're trying to accomplish, etc., thanks.

                • Re: do - catch
                  stcocoa Level 1 Level 1 (0 points)

                  HI , KMT

                   

                  I just want to know general stuff for all code.

                   

                  I want to try local DB transaction, and if there is no error, I want to do next things.

                  (this DB dose not give me callback ..so..)

                   

                  SO, I wanted to use, do-try-catch  , but maybe I have to use do-try-catch and else    ..

                   

                  and I put 'next things' in 'else'.

                    • Re: do - catch
                      Claude31 Level 4 Level 4 (815 points)

                      If there is no error, you will continue execution at instruction after catch.

                       

                      What do you different from this behavior ?

                        • Re: do - catch
                          stcocoa Level 1 Level 1 (0 points)

                          Oh !!!

                           

                          you mean

                           

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

                           

                          In above code,  B ()  or C()  will be strted after A() is 'ALL finished' ??

                            • Re: do - catch
                              eskimo Apple Staff Apple Staff (6,075 points)

                              Consider this code:

                              {
                                  defer {
                                      C1()
                                  }
                                  do {
                                      try A()
                                      C2()
                                  } catch {
                                      C3()
                                      B()
                                  }
                                  C4()
                              }
                              

                              Here’s how things pan out:

                              • C1 will run no matter what happens with the remaining code

                              • C2 will run if A does not throw

                              • C3 will run if A throws

                              • C4 will run if:

                                • A does not throw

                                • A throws but B catches the error

                              Any questions?

                              Share and Enjoy

                              Quinn “The Eskimo!”
                              Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                              let myEmail = "eskimo" + "1" + "@apple.com"

                                • Re: do - catch
                                  stcocoa Level 1 Level 1 (0 points)

                                  Perfect

                                   

                                  Only one question.

                                   

                                  C() will run in end of order always? Even after C4() ?

                                   

                                  Thanks ^^

                                    • Re: do - catch
                                      eskimo Apple Staff Apple Staff (6,075 points)

                                      C() will run in end of order always? Even after C4() ?

                                      I presume you mean C1.  There is no C in my example.

                                      And if that’s the case then you’ll find the answer in The Swift Programming Language, which says:

                                      A defer statement is used for executing code just before transferring program control outside of the scope that the deferstatement appears in.

                                      In my example “the scope that the defer statement appears in” ends with the curly bracket on line 13, after the call to C4 on line 13.

                                      ps Also note this TSPL quote:

                                      If multiple defer statements appear in the same scope, the order they appear is the reverse of the order they are executed.

                                      Share and Enjoy

                                      Quinn “The Eskimo!”
                                      Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                      let myEmail = "eskimo" + "1" + "@apple.com"

                          • Re: do - catch
                            Claude31 Level 4 Level 4 (815 points)

                            vvery interesting answer.

                             

                            butI do not understand C4 case. If B catches, why is C4 executed and not only the defer ?

                              • Re: do - catch
                                QuinceyMorris Level 6 Level 6 (2,465 points)

                                Because if an error is caught, execution continues with the statement after the do/catch construct.

                                 

                                IOW, once an error is caught, the sequence of execution returns to normal.

                                 

                                Note, for completeness, there are two cases where this is not true, for obvious reasons:

                                 

                                1. If a "try" statement within the catch block throws.

                                 

                                2. If the catch block executes a "throw" statement.