Try Catch...need my brain rattled.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • darrel

    Try Catch...need my brain rattled.

    Trying to get back into .net again after being out of it for a while.

    I'm trying to figure out the proper way to handle multiple events via a try
    catch.

    What I'm confused about is the proper method to handle, say, 3 separate
    events: A, B, and C. I only want all 3 to execute if all 3 can successfully
    execute.

    Is that what a try-catch is for, or is a try/catch mainly for individual
    events?

    Is there a best-practice way to handle checking for 3 separate events and
    only executing all 3 only if they all can execute without error?

    -Darrel

  • Harry Simpson

    #2
    Re: Try Catch...need my brain rattled.

    You could have three actions within a try catch even with multiple catches
    If any fail it would go directly to the catch and you could pop outta the
    procedure at that point.


    "darrel" <notreal@notrea l.comwrote in message
    news:%23DwjWtD7 IHA.1204@TK2MSF TNGP04.phx.gbl. ..
    Trying to get back into .net again after being out of it for a while.
    >
    I'm trying to figure out the proper way to handle multiple events via a
    try catch.
    >
    What I'm confused about is the proper method to handle, say, 3 separate
    events: A, B, and C. I only want all 3 to execute if all 3 can
    successfully execute.
    >
    Is that what a try-catch is for, or is a try/catch mainly for individual
    events?
    >
    Is there a best-practice way to handle checking for 3 separate events and
    only executing all 3 only if they all can execute without error?
    >
    -Darrel

    Comment

    • Scott M.

      #3
      Re: Try Catch...need my brain rattled.

      Try...Catch doesn't handle events, it handles exceptions raised by your
      code.

      If you want to write a common event handler (one procedure that is called
      when multiple events fire), you need to register the procedures as event
      handlers. In VB .NET, it's easy: just extend the "Handles" clause of an
      existing event handler with a comma and list the other events you wish to
      handle:

      Public Sub multiEventHandl er(ByVal sender As System.Object, e As EventArgs)
      _
      Handles Button1.Click, Button2.Click, Button3.Click

      In C#, it's a bit more involved. You'd need to go to the Page's Init event
      and register the procedures as handlers for a particular events:

      button1.Click += button_click()
      button2.Click += button_click()
      button3.Click += button_click()

      -Scott

      "darrel" <notreal@notrea l.comwrote in message
      news:%23DwjWtD7 IHA.1204@TK2MSF TNGP04.phx.gbl. ..
      Trying to get back into .net again after being out of it for a while.
      >
      I'm trying to figure out the proper way to handle multiple events via a
      try catch.
      >
      What I'm confused about is the proper method to handle, say, 3 separate
      events: A, B, and C. I only want all 3 to execute if all 3 can
      successfully execute.
      >
      Is that what a try-catch is for, or is a try/catch mainly for individual
      events?
      >
      Is there a best-practice way to handle checking for 3 separate events and
      only executing all 3 only if they all can execute without error?
      >
      -Darrel

      Comment

      • darrel

        #4
        Re: Try Catch...need my brain rattled.

        You could have three actions within a try catch even with multiple catches
        If any fail it would go directly to the catch and you could pop outta the
        procedure at that point.
        Right, but that would merely check them sequentially, correct? for example:

        Try
        A()
        B()
        C()
        catch
        end try

        A and B could execute even if C fails?

        -Darrel

        Comment

        • darrel

          #5
          Re: Try Catch...need my brain rattled.

          Try...Catch doesn't handle events, it handles exceptions raised by your
          code.
          Sorry...used a bad word there. I wasn't referring to event handlers but
          rather just executing code. Say I had 3 functions that do something. I only
          want all 3 to do what they do only if the other 2 can also do what they do.

          The more I think about this, the more I realize it's not any sort of
          automated thing and that it's just me checking for each individually,
          try/catch each one, and then rollback whatever I did if anyone of the 3
          fails.

          -Darrel

          Comment

          • Scott M.

            #6
            Re: Try Catch...need my brain rattled.

            You could just simply have each procedure return a boolean indicating
            success. That way you'll know if the procedure was successful before
            invoking the next one.


            "darrel" <notreal@notrea l.comwrote in message
            news:%23fC947D7 IHA.2220@TK2MSF TNGP06.phx.gbl. ..
            >Try...Catch doesn't handle events, it handles exceptions raised by your
            >code.
            >
            Sorry...used a bad word there. I wasn't referring to event handlers but
            rather just executing code. Say I had 3 functions that do something. I
            only want all 3 to do what they do only if the other 2 can also do what
            they do.
            >
            The more I think about this, the more I realize it's not any sort of
            automated thing and that it's just me checking for each individually,
            try/catch each one, and then rollback whatever I did if anyone of the 3
            fails.
            >
            -Darrel

            Comment

            • Scott M.

              #7
              Re: Try Catch...need my brain rattled.

              Do you have control over the source code of A, B, and C?

              If you do, I think you are thinking about this scenario incorrectly. If you
              are the one coding A, B, and C, you would have those procedures doing the
              try...catch and if an exception is encountered in those procedures, catch it
              and return False from the method. Otherwise return true.

              Then your calling procedure can just do:

              If A then
              If B then
              C
              End If
              End If

              If this is a matter of rolling back if you can only get so far, you should
              consider using Transactions to help automate that.

              "darrel" <notreal@notrea l.comwrote in message
              news:ukZzO7D7IH A.2348@TK2MSFTN GP06.phx.gbl...
              >You could have three actions within a try catch even with multiple
              >catches If any fail it would go directly to the catch and you could pop
              >outta the procedure at that point.
              >
              Right, but that would merely check them sequentially, correct? for
              example:
              >
              Try
              A()
              B()
              C()
              catch
              end try
              >
              A and B could execute even if C fails?
              >
              -Darrel

              Comment

              • darrel

                #8
                Re: Try Catch...need my brain rattled.

                Then your calling procedure can just do:
                >
                If A then
                If B then
                C
                End If
                End If
                That makes sense, but, again, that seems sequential. What if C fails? I have
                still done A and B, right?
                If this is a matter of rolling back if you can only get so far, you should
                consider using Transactions to help automate that.
                Yes. I agree. And it sounds like you are validating this. Let SQL handle
                what it does best and not try to do it all in the .net code. ;o)

                -Darrel


                Comment

                • Anthony Jones

                  #9
                  Re: Try Catch...need my brain rattled.

                  "darrel" <notreal@notrea l.comwrote in message
                  news:O3qOcgE7IH A.1080@TK2MSFTN GP06.phx.gbl...
                  Then your calling procedure can just do:

                  If A then
                  If B then
                  C
                  End If
                  End If
                  >
                  That makes sense, but, again, that seems sequential. What if C fails? I
                  have
                  still done A and B, right?
                  >
                  If this is a matter of rolling back if you can only get so far, you
                  should
                  consider using Transactions to help automate that.
                  >
                  Yes. I agree. And it sounds like you are validating this. Let SQL handle
                  what it does best and not try to do it all in the .net code. ;o)
                  >
                  What do these functions actually do?

                  It sounds like what you are after is a transactional system whereby the
                  effects of A, B and C must only be commited if all three are successful.
                  If the effects are changes to a database (or multiple databases that support
                  DTC) then its simple enough to enlist all the DB operations into a
                  containing transaction.

                  OTH, if the effects are on other sorts of resources you will need to find a
                  way to rollback changes done so far if a later operation fails.

                  Pratically then what you want is possible if you can enlist all the
                  operations into a containing transaction.


                  --
                  Anthony Jones - MVP ASP/ASP.NET


                  Comment

                  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                    #10
                    Re: Try Catch...need my brain rattled.

                    darrel wrote:
                    Trying to get back into .net again after being out of it for a while.
                    >
                    I'm trying to figure out the proper way to handle multiple events via a
                    try catch.
                    >
                    What I'm confused about is the proper method to handle, say, 3 separate
                    events: A, B, and C. I only want all 3 to execute if all 3 can
                    successfully execute.
                    >
                    Is that what a try-catch is for, or is a try/catch mainly for individual
                    events?
                    >
                    Is there a best-practice way to handle checking for 3 separate events
                    and only executing all 3 only if they all can execute without error?
                    >
                    -Darrel
                    You can't do that using merely a try...catch. You either need a way to
                    tell if a method will be able to execute before actually executing it,
                    or a way to rollback the method.

                    Either:

                    if (ACanExecute() && BCanExecute() && CCanExecute()) {
                    A();
                    B();
                    C();
                    }

                    or:

                    try {
                    A();
                    try {
                    B();
                    try {
                    C();
                    } catch (SomeException ex) {
                    RollBackA();
                    RollBackB();
                    RollBackC();
                    }
                    } catch (SomeException ex) {
                    RollBackB();
                    RollBackA();
                    }
                    } catch (SomeException ex) {
                    RollBackA();
                    }

                    or:

                    if (A()) {
                    if (B()) {
                    if (!C()) {
                    RollBackC();
                    RollBackB();
                    RollBackA();
                    }
                    } else {
                    RollBackB();
                    RollBackA();
                    }
                    } else {
                    RollBackA();
                    }

                    --
                    Göran Andersson
                    _____
                    Göran Anderssons privata hemsida.

                    Comment

                    • darrel

                      #11
                      Re: Try Catch...need my brain rattled.

                      What do these functions actually do?

                      Well my real world example was a file system update + a DB update. In
                      reality, the try-catch will be fine, as if the DB fails then the page likely
                      isn't loading at all, so I can mainly just check for the file system
                      function in the try-catch. If it fails, the DB update doesn't happen.

                      But then that got be thinking hypothetically of exactly what you decribe:
                      It sounds like what you are after is a transactional system whereby the
                      effects of A, B and C must only be commited if all three are successful.
                      Indeed, at that point, hopefully I'd be doing everything within SQL and
                      leverage transactions.

                      Otherwise, as Goran states, I write a bunch of code. ;o)

                      -Darrel


                      Comment

                      • darrel

                        #12
                        Re: Try Catch...need my brain rattled.

                        You can't do that using merely a try...catch. You either need a way to
                        tell if a method will be able to execute before actually executing it,
                        or a way to rollback the method.
                        Thanks! That confirms my line of thinking!

                        _Darrel

                        Comment

                        Working...