The exception is not throwing (in Win7?)

I have an application that reads in data from a text file. I recently realized that I had to do a data check for the data in a text file to see if I can display / process it correctly.

In principle, at the moment, the entire check is performed if the data is in the correct format, i.e. double is double, int is int, etc. And if it's not me, I chose an exception.

Same:

private static string CheckDouble(string doublevar)
        {
            double tryParseDoubleResult;
            var tryParseDouble = double.TryParse(doublevar, out tryParseDoubleResult);

            if (tryParseDouble)
            {
                return doublevar;
            }

            throw new Exception("Invalid data found. Cannot open.");
        }

That would be great. Except for the following:

  • I built this in a Win 7 environment in VS 2008.
  • I tested in Win7 / XP
  • Win7 - . , , , , . , , . Debug, , Release.
  • XP .

, , , , , , , .

Win7?


, , , , , . , ? Try? ( , XP, ...)

, 3 . - "". .

var w = new Window1();
w.Show();

// FormLoad tabitems 1, . , , , , Close(); , - , - .

Close();

edit1:

, , Singleton, , ...

edit2:

, . : , , . , . . , , , Environment.Exit(1); . .

...

, , , XP ( ), Win7 - .

+3
4

-, . System.Exception - , . , , DataFormatException - .

, , , , System.Exception - , . , , System.Exception . , . , DataFormatException ArgumentException ( , "doublevar" ). :

/// <summary>
/// Checks to see if the specified value is a double.
/// </summary>
/// <param name="valueToCheck">The value to check.</param>
/// <exception cref="ArgumentException">If <paramref name="valueToCheck"/>
/// is null or empty.</exception>
/// <exception cref="DataFormatException">If <paramref name="valueToCheck"/>
/// could not be parsed as a valid double.</exception>
private static double CheckDouble(string valueToCheck)
{
    if (string.IsNullOrEmpty(valueToCheck))
        throw new ArgumentException("Argument 'valueToCheck' cannot be null or empty.", "valueToCheck");

    double result;

    if (double.TryParse(valueToCheck, out result))
    {
        return result;
    }

    throw new DataFormatException("Value '" + valueToCheck + "' could not be parsed as a double.");
}

, ...

+2

, tryparse , , ... Debug.Print , , , ,

, , , try/catch, .

+1

, , , ? ? , throw new Exception(), , F10? , .

- , , ... , :

void Form_Load(object sender, EventArgs e)
{
    try
    {
        ParseFile();
    }
    catch(Exception ex)
    {
        MessageBox.Show("The file was not valid: " + ex.Message);
    }
}
+1

Most likely, your computers differ in their regional settings. For example, thousands / decimal separators differ between countries, for example, "1000.00" versus "1.000.00", DateTimeformats differ, etc. Try using the Parse / TryParse / ToString overloads that explicitly indicate CultureInfo, and avoid the default computer settings, possibly using CultureInfo.InvariantCulture.

0
source

Source: https://habr.com/ru/post/1751488/


All Articles