我想要一个ApplicationException异常,它只会在输入不是数字时显示消息。这是我现在所拥有的:只显示来自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."
)
}
}
}
}
对于它什么原因,什么,你可以请修复格式。 –
这让我很头疼,但我已经纠正它。每次我粘贴代码,然后选择,然后按代码格式化按钮,它会进入双倍间距......奇怪。 – HelpNeeder