Windows

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • TryParse vs. Exception

    6 answers - 3579 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hmmm, I think it also depends on what you 'expect' the app to be doing
    If you think that in most cases the string will be an integer, then there is little overhead using the exception method, only when a non-integer string is passed will the exception be invoked. If however, there is a good chance that the strings being processed may not be integers then it's a bad idea, as the exceptions are expensive in this case.
    Also one other thing to consider is what the TryParse function actuall does (I don't know this myself) but it could use exceptions internally, and just return false if it catches one
    There's my 2 pence worth :)
    Matt
    Message
    From: CSharpNET (AT) yahoogroups (DOT) com [mailto:CSharpNET (AT) yahoogroups (DOT) com] Behalf Paul Cowan
    Sent: 16 November 2005 13:42
    To: CSharpNET (AT) yahoogroups (DOT) com
    Subject: Re: [C#.NET] RE: TryParse vs. Exception
    The general consensus says you should never test a condition with an exception.
    I do say that somewhat hypocritically. I recently wanted to test a date's validity and the best I came up with was the following:
    try
    {
    string[] dateParts = DateTime.Parse(etaString, Root.UiCultureInfo,).ToString("HH:mm
    UTC, dd-MM-yy").Split(',');
    }
    catch
    {
    RenderTd(_etaTd, status, etaString); }
    Where I am indeed doing the complete opposite. Has anyone a nice solution to this.
    Paul
    dagda1 (AT) hotmail (DOT) com

    >From: David Lanouette <David.Lanouette (AT) gmail (DOT) com>
    >Reply-To: CSharpNET (AT) yahoogroups (DOT) com
    >To: CSharpNET (AT) yahoogroups (DOT) com
    >Subject: Re: [C#.NET] RE: TryParse vs. Exception
    >Date: Wed, 16 Nov 2005 08:33:40 -0500
    >
    >I don't have a specific answer, but I know that Exceptions are
    >generally expensive. I would /guess/ that the TryParse method would be
    >the better preformer.
    >
    >

    11/16/05, Peter H <peter.hartlen (AT) sib (DOT) sewrote:
    Hi!
    Is there a known difference in performance between checking if a
    string
    >is

    Numeric (integer in this case) using Double.TryParse() or catching
    an exception?
    i.e.
    try
    {
    return Convert.ToInt32("234a234"); } catch( FormatException ) {
    return Int.MinValue;
    }
    vs.
    double dTmpNum;
    if( Double.TryParse( Double.TryParse(sNumber, NumberStyles.Integer,
    NumberFormatInfo.CurrentInfo, out dTmpNum)) ) {
    return Convert.ToInt32(dTmpNum); } else {
    return Int.MinValue;
    }
    Thanks,
    Peter
    >
    >
    >

    [Non-text portions of this message have been removed]
    >
    >
    >
    >
    >

    Yahoo! Groups Links
    >
    >
    >
    >
    >
    >
    >
    >
    >


    >- David Lanouette
    >- David.Lanouette (AT) GMail (DOT) com

    Yahoo! Groups Sponsor Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page
    Yahoo! Groups Links
    Yahoo! Groups Sponsor
    Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page
    Yahoo! Groups Links
    <*To visit your group on the web, go to:
    <*To unsubscribe from this group, send an email to:
    CSharpNET-unsubscribe (AT) yahoogroups (DOT) com
    <*Your use of Yahoo! Groups is subject to:
  • No.1 | | 900 bytes | |

    Peter,

    According to the documentation

    TryParse does the work of Parse, but returns the result in an out parameter
    and the result is true or false based on the success of the attempt instead
    of throwing and exception. In your second example you are using
    Double.TryParse and then if the try is successful, then you are converting
    the double to an int. This is converting the value twice. This will probably
    give you more of a performance hit then your first example, but the Int32
    type also has a TryParse. If you use that method you will only be doing the
    parsing once, without the overhead of the exception. IMH this is definitely
    the way to go.

    Try this code:

    int dTmpNum;
    if( Int32.TryParse( sNumber, NumberStyles.Integer,
    NumberFormatInfo.CurrentInfo, out dTmpNum ) )
    {
    return dTmpNum;
    }
    else
    {
    return Int.MinValue;
    }
  • No.2 | | 1665 bytes | |

    If you're familiar with VB6, it's similar to the IsNumeric function. It
    checks if a value is numeric before it attempts the cast, but has the
    additional functionality of returning the double (final parameter) if the
    cast is valid.

    I found this article by googling Double.TryParse benchmark.
    http://aspalliance.com/80

    It seems that it is little more than wrapped Try{}Catch{} block, as the
    performance was nearly the same. My guess is it's a helper function that
    will allow more concise code.

    What's interesting to me is the incremental char method.

    11/16/05, Peter B <peter.hartlen (AT) sib (DOT) sewrote:

    In CSharpNET (AT) yahoogroups (DOT) com, "Matt Evans" < matt@twrote:
    Also one other thing to consider is what the TryParse function
    actuall does (I don't know this myself) but it could use exceptions
    internally, and just return false if it catches one

    Exactly!

    That's why I am hoping someone knows this for a fact.

    / Peter
    >
    >
    >
    >
    >
    >
    >

    Yahoo! Groups Links
    >
    >
    >
    >
    >
    >
    >


    [Non-text portions of this message have been removed]

    Yahoo! Groups Sponsor
    Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page

    Yahoo! Groups Links

    <*To visit your group on the web, go to:

    <*To unsubscribe from this group, send an email to:
    CSharpNET-unsubscribe (AT) yahoogroups (DOT) com

    <*Your use of Yahoo! Groups is subject to:
  • No.3 | | 4675 bytes | |

    yeah sorry - I didn't edit my email before I sent. With a NUMBER it's
    nearly the same with a non-number, TryParse is faster.

    apologies.

    11/16/05, Peter H <peter.hartlen (AT) sib (DOT) sewrote:

    Hello Sam!

    Not really, I also read the article and it says exactly the opposit. The
    performance hit of try-catch is a lot greater than what you get using
    Double.TryParse. I'm eager to agree with Brett's statment that
    Double.TryParse is definitely created to prevent the exception overhead
    when
    checking numbers.

    the other hand, Visual Basic's IsNumeric function is a simple wrapper
    using try-catch itself. This is what I have read and the tests I did
    confirms the theory.

    Also, John, that link you provided to Int32.TryParse, is for WinFX SDK.
    (which I don't really know what it is).
    Int32.TryParse is not part of the standard .NET Framework v. 1.1 is it?

    __ So how about getting that string to an Integer (not double) __
    I used the code inte the article <http://aspalliance.com/80>
    http://aspalliance.com/80 and also created a function (that uses
    Double.TryParse) to validate the string as an integer AND convert the
    resulting double into an integer. The performance hit of the conversion
    was
    very small, and compared to using try-catch it's a lot faster!

    BUT! To my horror I discovered that the use of

    double.TryParse(strInputVal,
    NumberStyles.Integer,
    NumberFormatInfo.CurrentInfo,
    out dVal);

    Does not return false if the value is above Int32.MaxValue, it seems like
    it
    simply makes sure strInputVal consists of numbers only (not how many)
    could this be true?

    / Peter
    --
    Message
    From: CSharpNET (AT) yahoogroups (DOT) com [mailto:CSharpNET (AT) yahoogroups (DOT) com]
    Behalf
    Sam Acheson
    Sent: Wednesday, November 16, 2005 5:02 PM
    To: CSharpNET (AT) yahoogroups (DOT) com
    Subject: Re: [C#.NET] RE: TryParse vs. Exception
    --
    If you're familiar with VB6, it's similar to the IsNumeric function. It
    checks if a value is numeric before it attempts the cast, but has the
    additional functionality of returning the double (final parameter) if the
    cast is valid.

    I found this article by googling Double.TryParse benchmark.
    http://aspalliance.com/80

    It seems that it is little more than wrapped Try{}Catch{} block, as the
    performance was nearly the same. My guess is it's a helper function that
    will allow more concise code.

    What's interesting to me is the incremental char method.

    11/16/05, Peter B <peter.hartlen (AT) sib (DOT) sewrote:

    In CSharpNET (AT) yahoogroups (DOT) com, "Matt Evans" < matt@twrote:
    Also one other thing to consider is what the TryParse function
    actuall does (I don't know this myself) but it could use exceptions
    internally, and just return false if it catches one

    Exactly!

    That's why I am hoping someone knows this for a fact.

    / Peter
    >
    >
    >
    >
    >
    >
    >

    Yahoo! Groups Links
    >
    >
    >
    >
    >
    >
    >
    >
    >

    [Non-text portions of this message have been removed]
    >
    >
    >
    >

    SPNSRED LINKS
    Programming
    <

    languages North
    <

    &america C
    <

    programming language
    Computer
    <

    programming
    languages

    YAH! GRUPS LINKS
    >
    >
    >

    * Visit your group "CSharpNET
    <" on the web.
    --
    * To unsubscribe from this group, send an email to:
    CSharpNET-unsubscribe (AT) yahoogroups (DOT) com
    <mailto:CSharpNET-unsubscribe@>
    --
    * Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
    <.
    --

    >
    >
    >
    >

    [Non-text portions of this message have been removed]
    >
    >
    >
    >
    >

    Yahoo! Groups Links
    >
    >
    >
    >
    >
    >
    >


    [Non-text portions of this message have been removed]

    Yahoo! Groups Sponsor
    Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page

    Yahoo! Groups Links

    <*To visit your group on the web, go to:

    <*To unsubscribe from this group, send an email to:
    CSharpNET-unsubscribe (AT) yahoogroups (DOT) com

    <*Your use of Yahoo! Groups is subject to:
  • No.4 | | 720 bytes | |

    WinFX is new. It's the combination of Avalon and Indigo but I'm not sure if
    it is still in beta or if it has been released as part of .NET 2.0. Either
    way, I think you're right about it not being a part of the 1.1 framework.

    I like the article that Brett dug up.

    [Non-text portions of this message have been removed]

    Yahoo! Groups Sponsor
    Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page

    Yahoo! Groups Links

    <*To visit your group on the web, go to:

    <*To unsubscribe from this group, send an email to:
    CSharpNET-unsubscribe (AT) yahoogroups (DOT) com

    <*Your use of Yahoo! Groups is subject to:
  • No.5 | | 907 bytes | |

    >Does not return false if the value is above Int32.MaxValue, it seems like it
    >simply makes sure strInputVal consists of numbers only (not how many)
    >could this be true?


    Remember, this function is checking for Doubles, not Ints. The
    NumberStyle just tells what type of double is allowed. So, looks like
    you have an extra step after converting to a double - check if it is
    between Int32.MinValue and Int32.MaxValue.

    - David Lanouette
    - David.Lanouette (AT) GMail (DOT) com

    Yahoo! Groups Sponsor
    Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page

    Yahoo! Groups Links

    <*To visit your group on the web, go to:

    <*To unsubscribe from this group, send an email to:
    CSharpNET-unsubscribe (AT) yahoogroups (DOT) com

    <*Your use of Yahoo! Groups is subject to:
  • No.6 | | 1310 bytes | |

    My current understanding is that Avalon / Indigo will not be in
    production release until the Vista release time frame, about a year from
    now. At that point it will bolt onto Windows XP/2003 as well.

    It's probably in some kind of beta and while I'm not sure I care that
    much about Avalon for line of business apps, any project needing
    interprocess communications and with a release time frame of 2007 or
    later should probably be architecting for Indigo at this point, since
    it's a completely different API from Remoting.

    John Johnson II wrote:

    >WinFX is new. It's the combination of Avalon and Indigo but I'm not sure if
    >it is still in beta or if it has been released as part of .NET 2.0. Either
    >way, I think you're right about it not being a part of the 1.1 framework.
    >
    >I like the article that Brett dug up.
    >


    Yahoo! Groups Sponsor
    Get fast access to your favorite Yahoo! Groups. Make Yahoo! your home page

    Yahoo! Groups Links

    <*To visit your group on the web, go to:

    <*To unsubscribe from this group, send an email to:
    CSharpNET-unsubscribe (AT) yahoogroups (DOT) com

    <*Your use of Yahoo! Groups is subject to:

Re: TryParse vs. Exception


max 4000 letters.
Your nickname that display:
In order to stop the spam: 3 + 2 =
QUESTION ON "Windows"

EMSDN.COM