1,105,214 Community Members

Custom Exception and messaging

Member Avatar
computerbear
Junior Poster in Training
69 posts since Oct 2010
Reputation Points: 3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Here I am again for my weekly Sunday night help. Sigh. How can I pass my error message into my exception? Thanks.

Let me know if you need to see more of the code.

/* Create an error message that is passed to the Exception class constructor for the Message property 
         * when a book does not meet the price-to-pages ratio. */
        public void displayErrorMessage()
        {
            string msg = "Ratio for " + Title + " is invalid. Price is " + Price + " for " + NumPages + " pages.";
        }


    }  //End Book Class
 
    /*Create a BookException class with a constructor that requires three (3) arguments for each book: 
       * a string title, a double price, and an int number of pages. */
    public class BookException : Exception
    {
        string displayErrorMessage(msg);
        public BookException(string Title, double Price, double NumPages)
            : base (msg)
        {
        }
        public BookException()
        {
        }
    }  //End BookException Class
Question Self-Answered as of 2 Years Ago
Member Avatar
C#Jaap
Junior Poster in Training
84 posts since Sep 2009
Reputation Points: 5 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

call

throw new BookException(Title, Price, Numpages);

declare

public class BookException : Exception
{
    public BookException(string Title, double Price, double NumPages)
            : base (String.Format("Ratio for {0} is invalid. Price is {1} for {2} pages.", Title, Price, Numpages))
    {
    }
}

Why not inherit from ApplicationException in stead of the more generic Exception: you exception is actually a very particular ApplicationException.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: