i didn't understand from the question if the user needs to enter data or not
can some one verify it with me
tried to follow the instructions,
and thats my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Book book1=new Book("Microsoft visual C#", "Joyce Farrell",40,120);
            Book book2=new Book("ASP.Net","Joyce Farrell",130,120);
            double price_Page =book2.price/book2.pages;
            try
            {
                if (price_Page > 10)
                {
                 throw new BookException(); //Throws the BookException
                }
            }
            catch (Exception ex)
            {
           label1.Text = (ex.ToString());
           
            }
              label1.Text ="The ratio is invalid";
        }   
    }
    public class BookException : Exception
    {        

         
        private static string msg = "The ratio is invalid.";    
        public BookException()            : base(msg)
        { 
  
        }  

    }
    public class Book
    {
            public string title;
            public string author;
            public double price;
            public int pages;
            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                pages = pag;
                price = pr;
            }

    }

}

Recommended Answers

All 39 Replies

The user does not need to enter data. But you will have to create 4 Book objects through code. These 4 book objects will test your Book class.

Otherwise, you are not quite on the right track. It seems you don't quite understand exceptions. You can go ahead and delete the BookException class (lines 39-49). You can also delete lines 23,36, 26-29. Finally, lines 21, 22 should be within the brackets of the Try block.
Once you have done that everything else should be good. But you will need to add your exception stuff.
In the constructor of the Book class is where you are going to throw an exception. So in that constructor you will have to determine whether the price per page is greater than 10 cents; if it is, then you need to execute the following line

throw new Exception("Pricer per page is too high...");

When all of that is done correctly, the following should occur. Your program creates a new book within the Try block. If the book's price per page is too high, the throw new Exception line of code will be executed from the constructor. Because the book was created in the Try block, the exception will be caught by the catch block, and the contents of the catch block will be executed.

thanks works good
i have another question when i click the bottons it shows the error massage but more stuff are showing like my path to the folder
how do i get it all to disappear

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 40, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 130, 120);
              
            }
            catch (Exception ex)
            {
                label1.Text = (ex.ToString());
              
            }
        }
        
        public class Book
        {

            public string title;
            public string author;
            public double price;
            public int pages;
           
           

            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                pages = pag;
                price = pr;

                if (pages > 10)
                {
                    throw new Exception("Pricer per page is too high...");
                }
            } 
        }
    
    }
}

Instead of ex.ToString(), use ex.Message

the name of the book is required, like the example ??

So in the following line:

throw new Exception("Pricer per page is too high...");

The string literal 'Pricer per page is too high...' can be replaced with any string you want. According to the assignment your resulting message should be something like:

"For GoodNightMoon, ratio is invalid. Price is $12.99 for 25 pages."

That string is made up of a combination of string literals and dynamic string values. For instance the name of the book, the price, and the number of pages are all values which can change during run time. While the 'For', ', ratio is invalid. Price is $', ' for ', and 'pages.' are all constant values.

So you need to concatenate (combine) a couple of dynamic string values with a couple of constant string values.

The syntax for doing something like that is the same as in the following example:

String errorMessage = "";
            String nameOfBook="BookName";
            double priceOfBook=12.99;

            errorMessage = "The name of the book is "+nameOfBook+" amd its price is $"+priceOfBook.ToString();

Once you have set the errorMessage string to whatever you need to have it, then you can send it to the Exception class constructor.

you need to put this code in the try code?

String errorMessage = "";
            String nameOfBook="BookName";
            double priceOfBook=12.99;

and this code in the "if" code in the class constructor?

errorMessage = "The name of the book is "+nameOfBook+" amd its price is $"+priceOfBook.ToString();

The changes you need to make will be in the Book class.
Otherwise the code I gave you is just an example of how to combine string literals and variables into a single string (in this case errorMessage). You will have to modify the code so that it has the correct text, and uses the right variable names (like the ones you have at the top of the Book class).

Start by instantiating a new string variable named errorMessage in the Book contructor. Then try to concatenate the other variables of the Book class and whatever string literals you need, into a single string which you will set to errorMesssage. then put the errorMessage variable in the Exception constructor parameter.

i fixed it like the example in the book can you tell me if there is other suggestions?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {try
            {Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.30, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912); }

            catch (Exception ex)
            { label1.Text = (ex.Message); }
        }
        public class Book
        {public string title;
            public string author;
            public double price;
            public int pages;
            
            public Book(string tit, string au, double pr, int pag)
            { this.title = tit;
                author = au;
                pages = pag;
                price = pr;

                if (pages > 0.10)
                { //throw new Exception("Price per page is too high...");
                    throw new Exception("For "+ tit + " ratio is invalid \nprice is "+ price.ToString("C")+" for "+ pages+" pages");
                }
          
            } 
        }
    
    }
}

Alright, that actually looks good. Well, I would suggest you fix the formatting, and also the if statement which causes the Exception to be thrown is not right. Remember you only want to throw the Exception when the price per page is greater than 10 cents. Right now it throws the Exception when there are more pages than 0.1, which is always going to be the case.

However, you are not done yet. There is one more part to the assignment which I ignored yesterday because it didn't make sense. And it still doesn't make much sense but we'll do it anyway.

The assignment also wants you to make a BookException class. This class will take three parameters in the constructor. A string for the title of the book. A double for the price of the book, and an int for the number of pages of the book. The only thing the BookException class will do is take those three values and create an string which will represent an error message (pretty much what you have already done, now you just need to do it inside a separate class). You will need to be able to get that string value from the BookException class and pass it to the constructor of Exception, when you throw it.

Here is what the BookException class is going to look like:

public class BookException
        {
            //instantiate a string variable called errorMessage here.

            //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
            public BookException()
            {
                //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
            }
        }

Once you have done everything for the BookException class, you will then have to reference the errorMessage variable from the BookException class and send it to the contructor of Exception.

throw new Exception(new BookException(tit,price,pages).errorMessage);

i kinda lost you can you help me fix it

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {try
            {Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.30, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912); }

            catch (Exception ex)
            {

                label1.Text = (ex.Message); 
            }
        }
        public class Book
        {public string title;
            public String author;
            public double price;
            public int pages;
        
            public Book(string tit, string au, double pr, int pag)
            {   title = tit;
                author = au;
                pages = pag;
                price = pr;

                if (price > 0.10)
                { //throw new Exception("Price per page is too high...");
                    throw new Exception(new BookException(title, price, pages).errorMessage);
                }
            }
            public class BookException
            {
                String errorMessage;
                //instantiate a string variable called errorMessage here.
               
                //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                public BookException(String title, double price, int pages )
                {

                    throw new Exception(new BookException(title, price, pages).errorMessage);
                      //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                }
                
            }

        }
    
    }
}

What you did is fine. You just need to concatenate the variables in the BookException constructor into a single string. And you need to change the if statement.

After that you are done.

To: if(price >0.10)?

it gives me errors

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.30, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912); }
 
            catch (Exception ex)
            { label1.Text = "For {1}"+ (ex.Message); }
          
        }
        public class Book
        {public string title;
            public string author;
            public double price;
            public int pages;

            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                pages = pag;
                price = pr;
                if (price > 0.10)
                {
                 throw new Exception(new BookException( title, price, pages).errorMessage);
                }   
            }
            public class BookException
            {
              public string errorMessage;
        
                public BookException(string title, double price, int pages)
                {
                    throw new Exception(new BookException(title, price, pages).errorMessage);
                }

            }
        }

    }
}

Yeah, the if statement if(price>0.10) is not going to do what you need it to do.
Otherwise, if you have errors you should start by structuring your code so that it is neat--like the way you would see it in the book. I would bet the error is in the constructor of your BookException class. You should be concatenating your error string there and passing it to Exception. Currently you are passing a BookException object to Exception.

can you demonstrate how to pass it ??

Something like this:

throw new Exception("This is an example string");

Where "this is an example string" should be your concatenated string.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.30, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912);
            }

            catch (Exception ex)
            { label1.Text = (ex.Message); }
        }
        public class Book
        {
            public string title;
            public string author;
            public double price;
            public int pages;

            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                pages = pag;
                price = pr;

                if (price > 0.10)
                {
                    throw new Exception("for  " + tit+ "ratio is invalid the price is "+ price.ToString("C")+ " for "+pages+"pages"  );
                }
                
            }
            public class BookException
            {
                //instantiate a string variable called errorMessage here.
                public string errorMessage;

                //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                public BookException(string title,  double price, int pages)
                {
                    if (price > 0.10)
                    {
                        throw new Exception(new BookException(title, price, pages).errorMessage);
                        //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                    }
                }
            }
        }
    }
}

Close. You need to remove the if statement from the BookException constructor. And you should only pass the errorMessage variable to the Exception constructor. But before you pass it you need to take the string you concatenated on line 48 and set the errorMessage variable to that string.
Finally you should be throwing the BookException in your Book class; right now you are throwing Exception.

BookExceptionDemoGUI.Form1.Book.BookException' does not contain a constructor that takes 1 arguments
thats whats the bold line giving me

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.30, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912);
            }

            catch (Exception ex)
            { label1.Text = (ex.Message); 
                 
            }
        }
        public class Book
        {
            public string title;
            public string author;
            public double price;
            public int pages;

            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                pages = pag;
                price = pr;

                if (price > 0.10)
                {
                   [B] throw new Exception(new BookException("for  " + tit+ "ratio is invalid the price is "+ price+ " for "+pages+" pages").errorMessage);[/B]
                     
                }
                
            }
            public class BookException
            {
                //instantiate a string variable called errorMessage here.
                public string errorMessage;

                //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                public BookException(string title,  double price, int pages)
                {
                   
                        throw new Exception(new BookException(title, price, pages).errorMessage);
                        //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                    
                }
            }
        }
    }
}

According to your code:

BookException(string title,  double price, int pages)

BookException expects three types of objects to be passed to its constructor. A string, a double, and an int.
The line you bolded shows that you are trying to pass a single String. So the compiler is trying to tell you that BookException does not have a constructor that takes a single string and nothing else.

Once you fix that, the string:

"for  " + tit+ "ratio is invalid the price is "+ price+ " for "+pages+" pages"

needs to be created inside your BookException class and not in your Book class.

i tried to change the way you showed me but my GUI form stuck
can you show me how it needs to be done ??

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {a
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.30, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912);
            }

            catch (Exception ex)
            { label1.Text = (ex.Message); 
                 
            }
        }
        public class Book
        {
            public string title;
            public string author;
            public double price;
            public int pages;

            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                price = pr;
                pages = pag;
  
                if (price > 0.10)
                {
                    throw new Exception(new BookException(title, price, pages).errorMessage);
                  
                     
                }
                
            }
            public class BookException
            {
                //instantiate a string variable called errorMessage here.
                public string errorMessage;
               
                
                //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                public BookException(string title,  double price, int pages)
                {


                       throw new Exception(new BookException("for"+title,price, pages).errorMessage);
                        //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                    
                }

         

             
            }
        }
    }
}

Get rid of line 67, then read line 68 and try to do it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.30, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912);
            }

            catch (Exception ex)
            { label1.Text = (ex.Message); 
                 
            }
        }
        public class Book
        {
            public string title;
            public string author;
            public double price;
            public int pages;

            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                price = pr;
                pages = pag;
  
                if (price > 0.10)
                {
                    throw new Exception(new BookException(title, price, pages).errorMessage);
                  
                     
                }
                
            }
            public class BookException
            {
                //instantiate a string variable called errorMessage here.
                public string errorMessage;
               
                
                //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                public BookException(string title,  double price, int pages)
                {

                      throw new Exception(new BookException(title, price, pages).errorMessage);
                        //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                    
                }

            }
        }
    }
}

On line 66, you are calling the BookException contructor from within the same BookException constructor. With your current code, this is going to result in an infinite loop.
You should be passing the errorMessage string to the Exception constructor. But before you pass that string you have to set it to something. The comment on line 67 explains what errorMessage should be set to.

there's improvement i got the name of the first book

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                Book book1 = new Book("Microsoft visual C#", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.30, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912);
            }

            catch (Exception ex)
            { label1.Text = (ex.Message + " invlid "); 
                 
            }
        }
        public class Book
        {
            public string title;
            public string author;
            public double price;
            public int pages;

            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                price = pr;
                pages = pag;
  
                if (price > 0.10)
                {
                    throw new Exception(new BookException(title, price, pages).errorMessage);
                  
                     
                }
                
            }
            public class BookException
            {
                //instantiate a string variable called errorMessage here.
                public string errorMessage;
               
                
                //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                public BookException(string title,  double price, int pages)
                {

                      throw new Exception(title+price+pages+errorMessage);
                        //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                    
                }

         

             
            }
        }
    }
}

Remove line number 66 this will call itself all the time and will result in an infinite loop

thats what i came with finally someone can verify it with me

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookExceptionDemoGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                Book book1 = new Book(" Microsoft visual C# ", "Joyce Farrell", 0.90, 120);
                Book book2 = new Book("ASP.Net", "Joyce Farrell", 0.40, 120);
                Book book3 = new Book("JAVA PROGRAMMING", "Joyce Farrell", 0.05, 120);
                Book book4 = new Book("C++: a beginner's guide", "Herbert Schildt", 0.09, 912);
            }

            catch (Exception ex)
            { label1.Text = ( ex.Message); }
        }
        public class Book
        {
            public string title;
            public string author;
            public double price;
            public int pages;

            public Book(string tit, string au, double pr, int pag)
            {
                this.title = tit;
                author = au;
                price = pr;
                pages = pag;
  
                if (price > 0.10)
                {
                    throw new Exception(new BookException(title, price, pages).errorMessage);    
                }
                
            }
            public class BookException
            {
                //instantiate a string variable called errorMessage here.
                public string errorMessage;
                
                //Change this constructor so it accepts three parameters (string title, a double price, and an int pages)
                public BookException(string title,  double price, int pages)
                {
                  throw new Exception("For "+title+" ratio is invalid\n...  price is " + price.ToString("C") +" for "+ pages +" pages "+ errorMessage);
                        //concatenate the three parameters together into a single string, like you did before, and set the errorMessage variable to it.
                }

            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.