开发者

Show only Message from ApplicationException

I am trying to have an ApplicationException exception which will show ONLY a message when input is not a number. Here is what I have right now:

static void getBookInfo(Book book)
{
    bool isNumeric;
    float number;
    string numberInput;

    Console.Write("Enter Book Title: ");
    book.Title = Console.ReadLine();
    Console.Write("Enter Author's First Name: ");
    book.AuthorFirstName = Console.ReadLine();
    Console.Write("Enter Author's Last Name: ");
    book.AuthorLastName = Console.ReadLine();
    Console.Write("Enter Book Price: $");
    numberInput = Console.ReadLine();

    isNumeric = float.TryParse(numberInput, out number);

    if (isNumeric)
        book.Price = number;
    else
    {
        throw new ApplicationException
        (
            "This is not a number!\n" +
            "Please try again."
        );  
    }
}

Whole Program.cs after edit which works. The problem was that ApplicationException part was displaying whole printout of the exception, now instead of doing that, it shows only message part. As usually it's something simple. :)

using System;

namespace Lab_6
{
    class Program
    {
        static void Main(string[] args)
        {
            Address address = new Address();
            address.StreetNumber = "800";
            address.StreetName = "East 96th Stre开发者_C百科et";
            address.City = "Indianapolis";
            address.State = "IN";
            address.ZipCode = "46240";

            Book book = new Book();

            try
            {
                getBookInfo(book);
                book.PublisherAddress = address;
                book.PublisherName = "Sams Publishing"; 

                Console.WriteLine("----Book----");
                book.display();
            }
            catch (NegativeInputException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            catch (ApplicationException ex)
            {
                Console.WriteLine(ex.Message);  // I had to change so I have only this, 
                                                // instead of whole printout.
                return;
            }
        }

        static void getBookInfo(Book book)
        {
            bool isNumeric;
            float number;
            string numberInput;

            Console.Write("Enter Book Title: ");
            book.Title = Console.ReadLine();
            Console.Write("Enter Author's First Name: ")
            book.AuthorFirstName = Console.ReadLine();
            Console.Write("Enter Author's Last Name: ");
            book.AuthorLastName = Console.ReadLine();
            Console.Write("Enter Book Price: $");
            numberInput = Console.ReadLine();

            isNumeric = float.TryParse(numberInput, out number);

            if (isNumeric)
                book.Price = number;
            else
            {
                throw new ApplicationException
                (
                    "This is not a number!\n" +
                    "Please try again."
                )   
            }
        }
    }
}


Exceptions don't show anything. That's up to the code that catches them.

Also, you should not use ApplicationException. Either use Exception, or use something more specific like FormatException.


If you just want to show a message to user don't throw any exceptions just show the message.

if (isNumeric)
{
    book.Price = number;
}
else
{
    MessageBox.Show("This is not a number!\n" + "Please try again.");
}

EDIT

If you really want to throw an exception and show its message. Use Exception.Message to display.

try
{
    getBookInfo(...)
}
catch (ApplicationException exception)
{
    MessageBox.Show(exception.Message);
}


The act of throwing an Exception and the act of catching the exception and displaying the error to the user are two separate pieces.

Your code for throwing the exception when the entered value is not a float is correct.

What you need to do is surround your call to the static getBookInfo method with a try{} catch{} that catches the exception and displays the message

try
{
     Book myBookParameter = .....;
     getBookInfo(myBookParameter);
}
catch(ApplicationException x)
{
     MessageBox.Show(x.Message);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜