2011-10-04 79 views
1

我想要一個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." 
       ) 
      } 
     } 
    } 
} 
+0

對於它什麼原因,什麼,你可以請修復格式。 –

+1

這讓我很頭疼,但我已經糾正它。每次我粘貼代碼,然後選擇,然後按代碼格式化按鈕,它會進入雙倍間距......奇怪。 – HelpNeeder

回答

3

例外不顯示任何內容。這取決於捕捉它們的代碼。您也可以使用ApplicationException。請使用Exception,或者使用更具體的內容,如FormatException

+0

但是,如何輸入非數字時只顯示消息描述?此時我正在顯示整個例外文本。我的指示是:輸入書價時,您的代碼應該檢查輸入的值是否爲數字。如果是,您可以將輸入的值分配給作爲參數傳遞的Book對象。如果不是,則應該拋出ApplicationException的對象 。 – HelpNeeder

+0

首先,你的指令並沒有說只顯示消息,他們說拋出一個異常(告訴你的教師看到[ApplicationException](http://msdn.microsoft.com/en-us/library/system .applicationexception.aspx) - 錯誤的異常,不應該再使用)。第二,如果你想顯示消息,那麼'Console.WriteLine(「message」);'? –

+0

這可能很簡單。本週我們正在處理異常處理,所以我認爲這是實踐。 – HelpNeeder

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); 
} 
+0

不! OP想拋出一個異常。 – adatapost

+0

我可以,但是,我特別要求拋出ApplicationException的對象。家庭作業... – HelpNeeder

+0

@HelpNeeder:你的問題是什麼? – CharithJ

2

拋出Exception的行爲和捕獲異常並向用戶顯示錯誤的行爲是兩個獨立的部分。

當輸入的值不是浮點數時拋出異常的代碼是正確的。

你需要做的就是圍繞你的靜態getBookInfo方法調用一個嘗試{}趕上{}來捕獲該異常,並顯示消息

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