de Morgan Who?
-
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; }
-
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; }
Mmm Discrete Structures... now that was a fun class:rolleyes:
// Steve McLenithan
-
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; }
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.
-
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.
: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.
-
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; }
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 -
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 nowActually, 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.
-
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; }
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 forif (obeyExisting) throw;
for the first test. -
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 forif (obeyExisting) throw;
for the first test.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.