Do we have "on error resume next" in C#?

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

    Do we have "on error resume next" in C#?

    Hi,



    I know that this is not a good practice, but I wonder do we have "on error
    resume next" in C#?



    Thanks,

    Max


  • ssamuel

    #2
    Re: Do we have "on error resume next" in C#?

    Max,

    No. Are you nuts? :)

    Seriously, no, there isn't, and all for the reasons that you'd expect.
    There's really a whole lot you can do with try/catch blocks. Carefully
    constructed code could do the same and more. Ask if you have a specific
    need that someone may be able to help with.


    Stephan


    Maxwell2006 wrote:
    Hi,
    >
    >
    >
    I know that this is not a good practice, but I wonder do we have "on error
    resume next" in C#?
    >
    >
    >
    Thanks,
    >
    Max

    Comment

    • Merlin

      #3
      Re: Do we have "on error resume next" in C#?

      I know that this is not a good practice, but I wonder do we have "on error
      resume next" in C#?
      Try..catch..fin ally is the best way... "on error resume next" would be
      something horrible :-)

      --

      //\/\\3rL1n_______


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Do we have "on error resume next" in C#?

        Maxwell2006 <alanalan@newsg roup.nospamwrot e:
        I know that this is not a good practice, but I wonder do we have "on error
        resume next" in C#?
        Fortunately not. Similar functionality can be obtained by using
        try/catch within a loop.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Tom Porterfield

          #5
          Re: Do we have &quot;on error resume next&quot; in C#?

          Maxwell2006 wrote:
          I know that this is not a good practice, but I wonder do we have "on error
          resume next" in C#?
          Fortunately, no we do not. You could emulate it by putting a try/catch
          around each line of code. By doing so you quickly see why "on error resume
          next" is such a bad idea in VB.
          --
          Tom Porterfield

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Do we have &quot;on error resume next&quot; in C#?

            Merlin <Merlin@LesFees .Netwrote:
            I know that this is not a good practice, but I wonder do we have "on error
            resume next" in C#?
            >
            Try..catch..fin ally is the best way... "on error resume next" would be
            something horrible :-)
            In fact, try/finally is usually better than try/catch/finally - there
            should generally be many more finally statements than catch statements,
            as that indicates you have centralised error handling. It's not always
            the case, but it's a good rule of thumb.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • sloan

              #7
              Re: Do we have &quot;on error resume next&quot; in C#?


              There was a "tension" when bringing vb6 to vb.net

              this was one of the thing brought over, even though it was kind of a bad
              thing.

              That's why it was a vb.net thing only, because c# didn't have to deal with
              the "upgrade" tension of vb6.

              I'm just trying to share the ~why as opposed to the yes/no of it.

              ..net 2.0 tries to ease more of this "tension" by using the My. keyword to
              wrap up some common functionality.




              "Maxwell200 6" <alanalan@newsg roup.nospamwrot e in message
              news:e%23ajBn2B HHA.4292@TK2MSF TNGP02.phx.gbl. ..
              Hi,
              >
              >
              >
              I know that this is not a good practice, but I wonder do we have "on error
              resume next" in C#?
              >
              >
              >
              Thanks,
              >
              Max
              >
              >

              Comment

              • Merlin

                #8
                Re: Do we have &quot;on error resume next&quot; in C#?

                Merlin <Merlin@LesFees .Netwrote:
                In fact, try/finally is usually better than try/catch/finally - there
                should generally be many more finally statements than catch statements,
                as that indicates you have centralised error handling. It's not always
                the case, but it's a good rule of thumb.
                I fully agree. But it's hard to give generic laws, all depends on the
                code style and a few variants can be accepted. try/finally or try/catch
                or try/catch/finally are there and are all powerfull ways to build
                code, but, of course, syntax elements are nothing if there's not a
                solid plan in the mind of the developer...

                --

                //\/\\3rL1n_______


                Comment

                • Tony Gravagno

                  #9
                  Re: Do we have &quot;on error resume next&quot; in C#?

                  Jon Skeet wrote:
                  >In fact, try/finally is usually better than try/catch/finally - there
                  >should generally be many more finally statements than catch statements,
                  >as that indicates you have centralised error handling. It's not always
                  >the case, but it's a good rule of thumb.
                  Merlin wrote:
                  >I fully agree. But it's hard to give generic laws, all depends on the
                  >code style and a few variants can be accepted. try/finally or try/catch
                  >or try/catch/finally are there and are all powerfull ways to build
                  >code, but, of course, syntax elements are nothing if there's not a
                  >solid plan in the mind of the developer...
                  Don't forget that we can have multiple catch blocks on a single try to
                  handle specific exceptions. That fact could inflate the number of
                  catches way past finally blocks. Does it seem like most people just
                  "catch (Exception ex)" as their first option?

                  On Error Resume Next can easily be implemented as:
                  try { foo; } finally {}
                  Sometimes you don't care if there is an exception thrown, like in a
                  final wrapup routine, you just want to do what you can and get out of
                  there as fast as possible.

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Do we have &quot;on error resume next&quot; in C#?

                    Tony Gravagno <g6q3x9lu53001@ sneakemail.com. invalidwrote:

                    <snip>
                    On Error Resume Next can easily be implemented as:
                    try { foo; } finally {}
                    Sometimes you don't care if there is an exception thrown, like in a
                    final wrapup routine, you just want to do what you can and get out of
                    there as fast as possible.
                    I'm not a VB programmer, but that doesn't sound like it's the same
                    thing at all. If foo throws an exception, then the above code will
                    effectively rethrow the exception - it *won't* resume execution on the
                    next line, which I *thought* was the behaviour of On Error Resume Next.

                    As I understand it, On Error Resume Next is closer to:

                    try { foo; } catch {}

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    • Tony Gravagno

                      #11
                      Re: Do we have &quot;on error resume next&quot; in C#?

                      Jon Skeet [C# MVP] wrote:
                      >Tony Gravagno wrote:
                      >On Error Resume Next can easily be implemented as:
                      >try { foo; } finally {}
                      >I'm not a VB programmer, but that doesn't sound like it's the same
                      >thing at all. If foo throws an exception, then the above code will
                      >effectively rethrow the exception - it *won't* resume execution on the
                      >next line, which I *thought* was the behaviour of On Error Resume Next.
                      >
                      >As I understand it, On Error Resume Next is closer to:
                      >
                      >try { foo; } catch {}
                      Yup yup, I was looking at the comments on Finally and should have
                      typed Catch. Duh, sorry.

                      Comment

                      • Michael D. Ober

                        #12
                        Re: Do we have &quot;on error resume next&quot; in C#?

                        It's more than that:

                        On Error Resume Next
                        Statement1
                        Statement2
                        Statement3

                        Translates to
                        try { Statement1; } catch {}
                        try { Statement2; } catch {}
                        try { Statement3; } catch {}

                        There are very few situations where On Error Resume Next is appropriate. I
                        use it primarily when I'm configuring the run time environment and some of
                        the configuration has already been done. Rather than test each
                        configuration item, I simply don't care if the item is already configured
                        and reconfiguring it throws an error. That said, you definitely have to
                        keep the On Error Resume Next modules in VB short and to the point.

                        C# doesn't have a direct equivalent to "On Error Resume Next".

                        Mike Ober.

                        "Tony Gravagno" <g6q3x9lu53001@ sneakemail.com. invalidwrote in message
                        news:j0hnl29dul ju3s3h76sae8aap chsk0ktmb@4ax.c om...
                        Jon Skeet [C# MVP] wrote:
                        >
                        >>Tony Gravagno wrote:
                        >>On Error Resume Next can easily be implemented as:
                        >>try { foo; } finally {}
                        >
                        >
                        >>I'm not a VB programmer, but that doesn't sound like it's the same
                        >>thing at all. If foo throws an exception, then the above code will
                        >>effectively rethrow the exception - it *won't* resume execution on the
                        >>next line, which I *thought* was the behaviour of On Error Resume Next.
                        >>
                        >>As I understand it, On Error Resume Next is closer to:
                        >>
                        >>try { foo; } catch {}
                        >
                        Yup yup, I was looking at the comments on Finally and should have
                        typed Catch. Duh, sorry.

                        Comment

                        Working...