仅显示ApplicationException中的消息

时间:2011-10-04 03:14:30

标签: c# .net winforms exception-handling

我正在尝试使用ApplicationException异常,当输入不是数字时,它只显示一条消息。这就是我现在所拥有的:

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."
        );  
    }
}

编辑后的整个Program.cs有效。问题是ApplicationException部分显示异常的整个打印输出,现在不是这样做,它只显示消息部分。通常它很简单。 :)

using System;

namespace Lab_6
{
    class Program
    {
        static void Main(string[] args)
        {
            Address address = new Address();
            address.StreetNumber = "800";
            address.StreetName = "East 96th Street";
            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."
                )   
            }
        }
    }
}

3 个答案:

答案 0 :(得分:3)

例外不显示任何内容。这取决于捕获它们的代码。

此外,您不应使用ApplicationException。使用Exception,或使用更具体的内容,例如FormatException

答案 1 :(得分:3)

如果您只想向用户显示消息,请不要抛出任何异常,只显示消息。

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

修改

如果你真的想抛出一个异常并显示它的消息。使用Exception.Message显示。

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

答案 2 :(得分:2)

抛出异常的行为以及捕获异常并向用户显示错误的行为是两个单独的部分。

当输入的值不是浮点值时抛出异常的代码是正确的。

您需要做的是使用try {} catch {}围绕您对静态getBookInfo方法的调用,捕获异常并显示消息

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