입력이 숫자가 아닌 경우에만 메시지를 표시하는 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."
);
}
}
편집 후 전체 프로그램 .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."
)
}
}
}
}
를 예외를 catch하고 표시하는 시도 {} 잡기 {}와 정적 getBookInfo 방법에 전화를 둘러싸고있다해야 할 일은, 당신이 만족 할 수 형식을 수정하십시오. –
이것은 두통을 줬지만, 나는 그것을 고쳤다. 코드를 붙여 넣을 때마다 다음을 선택하고 코드 서식 단추를 눌러 두 번 간격을 둡니다. 이상한. – HelpNeeder