Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw an error when the user enter null or empty string

I'm attempting to resolve the following exercise:

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");
            }
        }
    }
}

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?

like image 978
Pawel Avatar asked Sep 17 '15 13:09

Pawel


People also ask

How do you throw an exception if a string is null in Java?

NullPointerException is a RuntimeException. In Java, a special null value can be assigned to an object reference. NullPointerException is thrown when program attempts to use an object reference that has the null value.

Is an empty string null JS?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.

What happens if string in is null?

Example using a null check Very often in programming, a String is assigned null to represent that it is completely free and will be used for a specific purpose in the program. If you perform any operation or call a method on a null String, it throws the java. lang. NullPointerException.


2 Answers

Use String.IsNullOrEmpty Method (String). Change your set like this:

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

Also you can use String.IsNullOrWhiteSpace Method (String) that Indicates whether a specified string is null, empty, or consists only of white-space characters.

like image 119
Salah Akbari Avatar answered Oct 24 '22 14:10

Salah Akbari


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 

Your if statement is always true!

I would re-write it thus:

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

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");
}

As Dmitry Bychenko says, I didn't notice you were not testing for value. In getters you should use the value property. Not the name of your property


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

which in your case is the string "value":

throw new ArgumentException("Name cannot be null or empty string", "value");
like image 36
Liam Avatar answered Oct 24 '22 14:10

Liam