Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何处理int异常?_C#_Exception Handling - Fatal编程技术网

C# 如何处理int异常?

C# 如何处理int异常?,c#,exception-handling,C#,Exception Handling,我正在尝试处理输入字符串而不是int的情况。 比如说 newCustomer.PhoneNum = Convert.ToInt32(Console.ReadLine()); if (newCustomer.PhoneNum < 0 || newCustomer.PhoneNum > 10000000000 || newCustomer.PhoneNum.GetType() != typeof (int)) { throw new CustomException(newCust

我正在尝试处理输入字符串而不是int的情况。 比如说

newCustomer.PhoneNum = Convert.ToInt32(Console.ReadLine());
if (newCustomer.PhoneNum < 0 || newCustomer.PhoneNum > 10000000000 || newCustomer.PhoneNum.GetType() != typeof (int))
{
    throw new CustomException(newCustomer.PhoneNum.ToString());
}
newCustomer.PhoneNum=Convert.ToInt32(Console.ReadLine());
if(newCustomer.PhoneNum<0 | | newCustomer.PhoneNum>1000000000 | | | newCustomer.PhoneNum.GetType()!=typeof(int))
{
抛出新的CustomException(newCustomer.PhoneNum.ToString());
}

显然,if的最后一个条件不正确,但我没有主意。

您需要先使用int.TryParse检查输入的内容,如果它是有效整数,它会将值放入out参数,如果不是,则返回false

string text1 = "x";
int num1;
if (!int.TryParse(text1, out num1))
{
    // String is not a number.
}
int phoneNumber;
string input = Console.ReadLine();

if (!int.TryParse(input, out phoneNumber))
{
    throw new CustomException(input);
}
else if (phoneNumber < 0 || phoneNumber > 10000000000
{
    throw new CustomException(phoneNumber);
}

newCustomer.PhoneNum = phoneNumber;
int-phoneNumber;
字符串输入=Console.ReadLine();
如果(!int.TryParse(输入,输出电话号码))
{
抛出新的CustomException(输入);
}
如果(电话号码<0 | |电话号码>1000000000
{
抛出新的CustomException(电话号码);
}
newCustomer.PhoneNum=电话号码;

显然,我刚刚复制了您在示例中指定的验证逻辑,但它似乎有点简单,可能会抛出完全有效的电话号码。

您需要先使用int.TryParse检查输入的内容,如果是有效整数,它会将值放入out参数,如果不是,则返回false。

int phoneNumber;
string input = Console.ReadLine();

if (!int.TryParse(input, out phoneNumber))
{
    throw new CustomException(input);
}
else if (phoneNumber < 0 || phoneNumber > 10000000000
{
    throw new CustomException(phoneNumber);
}

newCustomer.PhoneNum = phoneNumber;
int-phoneNumber;
字符串输入=Console.ReadLine();
如果(!int.TryParse(输入,输出电话号码))
{
抛出新的CustomException(输入);
}
如果(电话号码<0 | |电话号码>1000000000
{
抛出新的CustomException(电话号码);
}
newCustomer.PhoneNum=电话号码;

显然,我刚刚复制了您在示例中指定的验证逻辑,但它似乎有点简单,可能会抛出完全有效的电话号码。

您需要先使用int.TryParse检查输入的内容,如果是有效整数,它会将值放入out参数,如果不是,则返回false。

int phoneNumber;
string input = Console.ReadLine();

if (!int.TryParse(input, out phoneNumber))
{
    throw new CustomException(input);
}
else if (phoneNumber < 0 || phoneNumber > 10000000000
{
    throw new CustomException(phoneNumber);
}

newCustomer.PhoneNum = phoneNumber;
int-phoneNumber;
字符串输入=Console.ReadLine();
如果(!int.TryParse(输入,输出电话号码))
{
抛出新的CustomException(输入);
}
如果(电话号码<0 | |电话号码>1000000000
{
抛出新的CustomException(电话号码);
}
newCustomer.PhoneNum=电话号码;

显然,我刚刚复制了您在示例中指定的验证逻辑,但它似乎有点简单,可能会抛出完全有效的电话号码。

您需要先使用int.TryParse检查输入的内容,如果是有效整数,它会将值放入out参数,如果不是,则返回false。

int phoneNumber;
string input = Console.ReadLine();

if (!int.TryParse(input, out phoneNumber))
{
    throw new CustomException(input);
}
else if (phoneNumber < 0 || phoneNumber > 10000000000
{
    throw new CustomException(phoneNumber);
}

newCustomer.PhoneNum = phoneNumber;
int-phoneNumber;
字符串输入=Console.ReadLine();
如果(!int.TryParse(输入,输出电话号码))
{
抛出新的CustomException(输入);
}
如果(电话号码<0 | |电话号码>1000000000
{
抛出新的CustomException(电话号码);
}
newCustomer.PhoneNum=电话号码;

显然,我刚刚复制了您在示例中指定的验证逻辑,但它似乎有点简单,可能会抛出完全有效的电话号码。

如果您使用的是原始数据,可以使用前面提到的int.TryParse()

但是,如果您试图验证更复杂的规则,您可能希望使用FluentValidation nuget包之类的东西,我非常喜欢它。它使验证成为一种更愉快的体验:)


或者,如果您使用的是ASP.Net或MVC之类的工具,则会有内置的验证引擎。在MVC中,您可以使用模型数据注释轻松地(以有组织的方式)执行客户端和服务器端验证。请查看。

如果您使用的是原始数据,则可以使用前面提到的int.TryParse()

但是,如果您试图验证更复杂的规则,您可能希望使用FluentValidation nuget包之类的东西,我非常喜欢它。它使验证成为一种更愉快的体验:)


或者,如果您使用的是ASP.Net或MVC之类的工具,则会有内置的验证引擎。在MVC中,您可以使用模型数据注释轻松地(以有组织的方式)执行客户端和服务器端验证。请查看。

如果您使用的是原始数据,则可以使用前面提到的int.TryParse()

但是,如果您试图验证更复杂的规则,您可能希望使用FluentValidation nuget包之类的东西,我非常喜欢它。它使验证成为一种更愉快的体验:)


或者,如果您使用的是ASP.Net或MVC之类的工具,则会有内置的验证引擎。在MVC中,您可以使用模型数据注释轻松地(以有组织的方式)执行客户端和服务器端验证。请查看。

如果您使用的是原始数据,则可以使用前面提到的int.TryParse()

但是,如果您试图验证更复杂的规则,您可能希望使用FluentValidation nuget包之类的东西,我非常喜欢它。它使验证成为一种更愉快的体验:)



或者,如果您使用的是ASP.Net或MVC之类的东西,则会有内置的验证引擎。在MVC中,您可以使用模型数据注释非常轻松地(以有组织的方式)进行客户端和服务器端验证。查看。

Int32.TryParse是您需要的。有多种方法可以处理异常和验证-在我们真正提供帮助之前,您需要完全清楚自己的情况以及使用的技术。请注意,将电话号码表示为整数通常不是最佳解决方案。在特殊情况下,您可能需要特殊字符,如+、#或*,前导零可能是有效的。32位整数是否足以满足您的需要?我在您的个人资料中看不到区域设置,但它的大小不足以容纳美国或英国的电话号码,而且如果前缀为国际国家代码,肯定无法容纳大多数号码。旁注:不要尝试将电话号码保存为int,这是一个坏主意。