We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,703 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Custom Exception and messaging

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
2
Contributors
1
Reply
1 Week
Discussion Span
2 Years Ago
Last Updated
2
Views
Question
Answered
computerbear
Junior Poster in Training
68 posts since Oct 2010
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
Question Self-Answered as of 2 Years Ago

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.

C#Jaap
Junior Poster in Training
63 posts since Sep 2009
Reputation Points: 15
Solved Threads: 11
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0578 seconds using 2.68MB