locked
weird problem with String.IsNullOrEmpty RRS feed

  • Question

  • User65893105 posted

    I have some code that checks a string for null or empty but its not working correctly

    if (string.IsNullOrEmpty(serializedValue))
                {
                    throw new ArgumentNullException("serializedValue",
                                                    string.Format("Parsing {0} : parameter cannot be empty or null",
                                                                  enumeratedType.Name));
                }
    serializedValue does contain a value, but the code always moves into the exception, it should skip past it.  Im using .net 4.5, has anyone else experienced this ?  Is there a root cause and fix ?

    Thursday, December 18, 2014 6:23 AM

Answers

  • User1577371250 posted

    I don't think it will go to Exception if it has a value.

    Please check the following.

    1. put a breakpoint for value  and check whether it has a value.

    string value = serializedValue;

    if (string.IsNullOrEmpty(serializedValue))
    {
    throw new ArgumentNullException("serializedValue", string.Format("Parsing {0} : parameter cannot be empty or null", enumeratedType.Name));
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 18, 2014 6:51 AM
  • User-760709272 posted

    It looks to me that the process you are debugging doesn't match the code in your Visual Studio.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 19, 2014 4:12 AM

All replies

  • User-760709272 posted

    If it contained a value it wouldn't go into the exception.  Do you *know* it has a value as you have checked in the debugger?  Or are you assuming it has a value?

    Thursday, December 18, 2014 6:34 AM
  • User1577371250 posted

    I don't think it will go to Exception if it has a value.

    Please check the following.

    1. put a breakpoint for value  and check whether it has a value.

    string value = serializedValue;

    if (string.IsNullOrEmpty(serializedValue))
    {
    throw new ArgumentNullException("serializedValue", string.Format("Parsing {0} : parameter cannot be empty or null", enumeratedType.Name));
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 18, 2014 6:51 AM
  • User65893105 posted

    ive stepped through it in the debugger, the variable does contain a value, strangely, even though it steps into the line, it doesn't actually raise the exception, then it steps into the correct branch of the code ! weird

    Friday, December 19, 2014 4:08 AM
  • User1577371250 posted

    Can you show the few more lines of code?

    Friday, December 19, 2014 4:11 AM
  • User-760709272 posted

    It looks to me that the process you are debugging doesn't match the code in your Visual Studio.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 19, 2014 4:12 AM
  • User1577371250 posted

    Yes, As Aidyf said you might be in the cached version of the application.

    Friday, December 19, 2014 4:15 AM
  • User-484054684 posted

    strangely, even though it steps into the line, it doesn't actually raise the exception, then it steps into the correct branch of the code ! weird

    From this statement, I believe, you don't have equivalent catch block in this part and so, the control goes to the previous file (caller) and if you have a catch block in that layer - put a breakpoint in the catch block, and you may see the control hitting the catch block.

    If there are multiple catch blocks in each layer and if you are unsure, I'd suggest to put breakpoints on each catch block and see. 

    Friday, December 19, 2014 6:23 AM