Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. de Morgan Who?

de Morgan Who?

Scheduled Pinned Locked Moved The Lounge
helpquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brady Kelly
    wrote on last edited by
    #1

    I can hardly believe I was nearly stumped by this simple piece of logic; if the exception number says it's an object that already exists, and I want to ignore the error, don't re-throw. However, the 'phrasing' made it look funny.

                catch(SqlException sx)
                {
                    // Ignore 'There is already an object named' exceptions.
                    if ((!ignoreExisting) || (sx.Number != 2714))
                    {
                        throw;
                    }
    
    S P L P 4 Replies Last reply
    0
    • B Brady Kelly

      I can hardly believe I was nearly stumped by this simple piece of logic; if the exception number says it's an object that already exists, and I want to ignore the error, don't re-throw. However, the 'phrasing' made it look funny.

                  catch(SqlException sx)
                  {
                      // Ignore 'There is already an object named' exceptions.
                      if ((!ignoreExisting) || (sx.Number != 2714))
                      {
                          throw;
                      }
      
      S Offline
      S Offline
      Steve McLenithan
      wrote on last edited by
      #2

      Mmm Discrete Structures... now that was a fun class:rolleyes:

      // Steve McLenithan

      1 Reply Last reply
      0
      • B Brady Kelly

        I can hardly believe I was nearly stumped by this simple piece of logic; if the exception number says it's an object that already exists, and I want to ignore the error, don't re-throw. However, the 'phrasing' made it look funny.

                    catch(SqlException sx)
                    {
                        // Ignore 'There is already an object named' exceptions.
                        if ((!ignoreExisting) || (sx.Number != 2714))
                        {
                            throw;
                        }
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Brady Kelly wrote:

        sx.Number != 2714

        Ooh - I just love magic numbers. Now, move along to the coding horrors - move along. ;)

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        B 1 Reply Last reply
        0
        • P Pete OHanlon

          Brady Kelly wrote:

          sx.Number != 2714

          Ooh - I just love magic numbers. Now, move along to the coding horrors - move along. ;)

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          B Offline
          B Offline
          Brady Kelly
          wrote on last edited by
          #4

          :laugh:  You missed my comment clearly stating the meaning of the number. I do so hate:

                      catch(SqlException sx)
                      {
                          // Ignore 'There is already an object named' exceptions.
                          if (!sx.Message.ToLower().Contains("there is already an object named"))
                          {
                              throw;
                          }
          

          Pits fall into Chuck Norris.

          1 Reply Last reply
          0
          • B Brady Kelly

            I can hardly believe I was nearly stumped by this simple piece of logic; if the exception number says it's an object that already exists, and I want to ignore the error, don't re-throw. However, the 'phrasing' made it look funny.

                        catch(SqlException sx)
                        {
                            // Ignore 'There is already an object named' exceptions.
                            if ((!ignoreExisting) || (sx.Number != 2714))
                            {
                                throw;
                            }
            
            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            Brady Kelly wrote:

            ((!ignoreExisting) || (sx.Number != 2714))

            = !(ignoreExisting && sx.Number == 2714)

            (you actually have way too many braces ;P !ignoreExisting || sx.Number != 2714 would be sufficient)

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 3 out now

            B 1 Reply Last reply
            0
            • L leppie

              Brady Kelly wrote:

              ((!ignoreExisting) || (sx.Number != 2714))

              = !(ignoreExisting && sx.Number == 2714)

              (you actually have way too many braces ;P !ignoreExisting || sx.Number != 2714 would be sufficient)

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 3 out now

              B Offline
              B Offline
              Brady Kelly
              wrote on last edited by
              #6

              Actually, you have too few braces; **(**!ignoreExisting || sx.Number != 2714**)** would be sufficient. :doh: I started with your first form of the expression, but C# requires the whole condition to be parenthesised, giving:

              (!(ignoreExisting && sx.Number == 2714))

              which I find rather ugly.

              Pits fall into Chuck Norris.

              1 Reply Last reply
              0
              • B Brady Kelly

                I can hardly believe I was nearly stumped by this simple piece of logic; if the exception number says it's an object that already exists, and I want to ignore the error, don't re-throw. However, the 'phrasing' made it look funny.

                            catch(SqlException sx)
                            {
                                // Ignore 'There is already an object named' exceptions.
                                if ((!ignoreExisting) || (sx.Number != 2714))
                                {
                                    throw;
                                }
                
                P Offline
                P Offline
                PaulLinton
                wrote on last edited by
                #7

                If the logic is difficult for the author to understand, have pity on anyone else who will read the code. How about if (!ignoreExisting) throw; if (sx.Number != 2714) throw; Personally, I find double negatives put my head in a spin, so I would go for if (obeyExisting) throw; for the first test.

                B 1 Reply Last reply
                0
                • P PaulLinton

                  If the logic is difficult for the author to understand, have pity on anyone else who will read the code. How about if (!ignoreExisting) throw; if (sx.Number != 2714) throw; Personally, I find double negatives put my head in a spin, so I would go for if (obeyExisting) throw; for the first test.

                  B Offline
                  B Offline
                  Brady Kelly
                  wrote on last edited by
                  #8

                  It's not difficult for the author to understand, it was just initially difficult for the author to factor expression. I like you first suggestion however, and were the code less personal, and more didactic as much of my professional code is, I would go back and use that.

                  Pits fall into Chuck Norris.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups