2011-07-31 129 views
3

我是C#編程的初學者。在使用表單構建應用程序時遇到小問題。我會盡力在我的能力和經驗中正確解釋它。當我試圖處理在我的Form1中實例化的Class1引起的異常時,問題得到了解決。假設我在Class1中有「public int Calc(int a,int b)」函數。在Form1中,我已經實例化了這個類來調用它的「Calc」函數。如果我想消息的錯誤(FE:除以零)我必須包裝函數調用到的try/catch元素:WinForm中的異常處理

// Form1中:

Class1 C1 = new Class1(); 
int a = 5; 
int b = 0; 
int c = 0; 

try{ 
    c = C1.Calc(a,b) 
} 
catch(DivideByZeroException e) 
{ 
    // some error handling code 
} 

...我認爲這個例子是不是適當的面嚮對象的技術,所以我不得不決定直接把try/catch語句元素到Class 1:

// Class1的:

public int Calc(int a, int b) 
{ 
    int c = 0; 
    try{ 
     c = a/b; 
    } 
    catch(DivideByZeroException e) 
    { 
     // ......... 
    } 
    return c; 
} 

...問題是,我怎樣才能獲得消息(DivideByZeroException E)爲我的形成1能夠處理它併發送消息。我不想在Form1中創建一些靜態函數,只是爲了從Class1中到達MessageBox類,因爲它沒有在適當的OOP功能和Class1的可重用性方面發揮作用。我已閱讀關於事件和委託(我理解的是簡單的指針,功能類似於C++),但它有點混亂,我沒有將這種技術應用到我的代碼中。你能否寫一個簡單的例子來指出我的正確方向。

謝謝你們

Cembo

回答

7

正確的技術真的是第一位的。如果你不能在你的功能中處理它,那麼你就沒有業務嘗試。將異常處理放在可以處理異常並且程序可以繼續(或正常退出)的地方,並以適當的方式通知用戶錯誤。

-4

一個簡單的方法可能是改變你的方法是這樣的

public int Calc(int a, int b, out string error) 
{ 
    error = string.empty; 
    int c = 0; 
    try 
    { 
     c = a/b; 
    } 
    catch(DivideByZeroException e) 
    { 
     error = e.ToString(); 
    } 
    return c; 
} 

現在在來電者可以檢查

string error; 
int res = Calc(1, 2, out error); 
if(!string.isnullorempty(error) 
{ 
    //error has occured 
    messagebox.show(error); 
} 
else 
{ 
    //no error 
    // you can proceed noramlly 
} 
+1

-1這通過了可怕的錯誤的返回值是錯誤代碼。 – siride

+0

你能解釋爲什麼這種方法可怕嗎? –

+1

現在調用Calc的任何函數都必須明確地處理錯誤(通過打印出一條消息或類似的消息)或將它傳遞給調用者。現在整個鏈必須傳遞錯誤消息。異常允許您在適當的地方處理錯誤,而不必將它沿整個調用鏈傳遞(容易出錯且乏味)。 – siride

1

爲了讓它泡成的範圍調用UI時,您需要在捕獲異常時拋出異常,或者更好的是不要拋出異常 - 因爲除以零將導致CLR在必要時拋出異常。你可以在這裏做簡單的檢查,而Calc API調用可能仍然會拋出異常。

例如,只是之前檢查數據的Calc電話:

if (a > 0 && b > 0) 
{ 
    var result = Calc(a, b); 
} 
else 
{ 
    //tell the user to input valid data 
} 

Calc方法,你可以做類似的檢查,並拋出一個相關的異常:

public int Calc(int a, int b) 
{ 
    if (a <= 0) throw new ArgumentException("appropriate message here"); 
    if (b <= 0) throw new ArgumentException("appropriate message here"); 
    ... 
} 

的想法在這裏是預防被零除的,但它可能是你的情況有點過分,因爲前面的例子顯示,你基本上可以提供相同的行爲,但是現在你需要捕獲異常:

try 
{ 
    var result = Calc(a, b); 
} 
catch //use appropriate exception catches 
{ 
    //tell the user to input valid data 
} 
+0

我喜歡這個,很好的防守編碼,就像你說在這種情況下有點過分:) +1 – AidanO

0
public int Calc(int a, int b) 
    { 
     int c = a/b; 
    } 

給它平原,你需要whataver邏輯簡單。

然後處理表單中的錯誤,你如何在第一時間做到了。

2

我建議你實現你的應用程序下面的代碼Program.cs文件中

[STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 

     // ----------------- 
     Application.ThreadException += Application_ThreadException; 
     // ----------------- 
    } 

    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
    { 
     // Handle your exception here... 
     MessageBox.Show(string.Format("There is an error\n{0}", e.Exception.Message)); 
    } 

這將趕上整個應用程序的未處理的異常。