Search code examples
c#classexceptionnullthrow

How can I catch null exceptions in "set" function of a class? (c#)


I'm supposed to write a program that lets the user enter the name, description and the number of pages of a book, and the program should catch exceptions if the name or description are null, or if the number of pages is lower than zero. The teacher said we need to catch the exceptions in the "set" function of the class, but I can't seem to get it right. This is what the class looks like:

class Book
{
    private string Name;
    private string Description;
    private int Pages;

    public string GetName()
    {
        return Name;
    }
    public string GetDescription()
    {
        return Description;
    }
    public int GetPages()
    {
        return Pages;
    }

    public void SetName(string Name)
    {
        if (this.Name == null)
            throw new Exception("The name can't be blank");
        else
            this.Name = Name;
    }

    public void SetDescription(string Description)
    {
        if (this.Description == null)
            throw new Exception("The description can't be blank");
        else
            this.Description = Description;
    }

    public void SetPages(int Pages)
    {
       if(Pages > 0)
        {
            this.Pages = Pages;
        }
       else
        {
            Console.WriteLine("Number of pages has to be higher than zero");
        }   
    }
    public void Write()
    {
        Console.WriteLine("Name: {0}, Description: {1}, Pages: {2}", Name, Description, Pages);
    }

}

And the main looks like this:

Book hp = new Book();
        hp.SetName("Harry Potter");
        hp.SetDescription("It's okay");
        hp.SetPages(-500);
        hp.Write();

I know the SetPages doesnt really use the catch method, but I think it still works (although if someone has an idea how to use the catch method there I'll be happy to hear). My problem is that even when the name and description strings clearly have input, the null exception still gets thrown. Anyone know how I can fix that? Any help would be appreciated.


Solution

  • You have a name clash. You are actually checking the private field, not the parameter that is passed on to your method.

    this.Name refers to the private field of your class, not the parameter. This is why proper naming conventions matter. Change the parameter to be lower case to avoid confusion and make sure you check that value for null:

    public void SetName(string name)
    {
        if (name == null)
            throw new Exception("The name can't be blank");
        else
            this.Name = name;
    }
    

    You may also want to consider using the static String function IsNullOrWhiteSpace:

    if (String.IsNullOrWhiteSpace(name))
        throw new Exception("The name can't be blank");
    

    There are also conventions around private fields so you might want to change the name of that one too. For instance, a common way of naming private fields is:

    private string _name;
    

    Your try/catch block is always being fired because you are always checking against the private field which is null. Once you correct the issue with the field, the check will be made against the parameter, the field will be set correctly and the try/catch block should not execute (unless of course, you pass in a null value).