当用户输入空或空字符串时发出错误 [英] Throw an error when the user enter null or empty string

查看:102
本文介绍了当用户输入空或空字符串时发出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决以下练习:

I'm attempting to resolve the following exercise:


您需要创建一个名为 Product 表示产品。
该类有一个名为 Name 的属性。 Product class
的用户应该可以获取并设置 Name 属性的值。但是,
任何尝试将 Name 的值设置为空字符串或null
值应引发异常。另外, Product class
的用户也不能访问 Product 的任何其他数据成员
类。您将如何创建这样的类?

You need to create a class named Product that represents a product. The class has a single property named Name. Users of the Product class should be able to get and set the value of the Name property. However, any attempt to set the value of Name to an empty string or a null value should raise an exception. Also, users of the Product class should not be able to access any other data members of the Product class. How will you create such a class?

我已经创建了以下代码,但由于某种原因,它不会在字符串时抛出异常无效:

I have created the following code but for some reason it does not throw the exception when the string is invalid:

class Program
{
    static void Main(string[] args)
    {
        Product newProduct = new Product();
        Console.WriteLine("Enter Product name:");
        newProduct.Name = null; //Console.ReadLine();
        Console.WriteLine("Product name is : {0}", newProduct.Name);
        Console.ReadLine();
    }
}

class Product
{
    private string name;
    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            if (Name != String.Empty || Name != null)
            {
                name = value;
            }
            else
            {
                throw new ArgumentException("Name cannot be null or empty string", "Name");
            }
        }
    }
}

是因为我没有 try-catch 语句,所以异常不会抛出
我也想知道是否有可能只有catch语句没有try语句?

Is the exception not thrown because I do not have try-catch statement? I was also wondering is it possible to have only catch statement without try statement?

推荐答案

您的如果状态是错误的。我们来做一个真相表:

Your if state is wrong. Let's do a truth table:

if (value != String.Empty || value != null)







Name = null   True Or False  = True 
Name = "name" True Or True = True
Name = ""     False Or True = True 

您的if语句始终为真

Your if statement is always true!

因此,我将重写:

if (value == String.Empty || value == null)
{
     throw new ArgumentException("Name cannot be null or empty string", "Name");  
}
else
{
     name = value;
}






你可以更改或者与AND,但我认为以上读数更好(以下有不必要的双重否定):


you could just change the Or to and AND but I think the above reads better (the below has an unnecessary double negative):

if (value != String.Empty && value != null)
{
   name = value;
}
else
{
    throw new ArgumentException("Name cannot be null or empty string", "value");
}






正如Dmitry Bychenko所说,我没有注意到你没有测试在吸烟者中,您应该使用属性。不是您的属性的名称

第二个参数(再次由Dmitry Bychenko指出)在你的例外应该是:

The second parameter (again pointed out by Dmitry Bychenko) in your exception should be:


导致当前异常的参数的名称。

The name of the parameter that caused the current exception.

MSDN

您的情况下是字符串value

throw new ArgumentException("Name cannot be null or empty string", "value");

这篇关于当用户输入空或空字符串时发出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆