2013-05-14 76 views
1

我有一個名爲TryMe的方法,它有try catch塊並捕獲他的異常。
我從另一個類中調用他,但是當發生異常時它不會停止代碼執行。
實施例:C#異常和代碼破壞

public void TryMe() 
{ 
    try 
    { 
     SomeMethod(); 
    } 
    catch(Exception exception){ 
     MessageBox.Show(exception.Message); 
    } 
} 


    //Method calling 
    Actions CAactions = new Actions(); 
    CActions.TryMe(); 
    /////////////////////////////////// 
    //If exception is handled it should stop to here. 

    this.Hide(); 
    FormActions FormActions = new FormActions(); 

的方法定義在類文件中。該方法調用是在窗體中。
問題是它只是顯示消息框並繼續執行代碼。
我想在異常捕獲之後停止代碼而不隱藏表單。如果一切順利,它應該隱藏它。
也許我的想法是錯誤的?

回答

7

最簡單的解決將是改變你的funcion返回真/假取決於它是否成功與否(即只隱藏表單,如果TryMe方法並沒有得到一個錯誤):

public bool TryMe() 
{ 
    try 
    { 
    SomeMethod(); 
    return true; 
    } 
    catch (Exception exception) 
    { 
    // log exception 
    return false; 
    } 
    } 

並調用它像這樣:

if (CActions.TryMe()) 
{ 
    this.Hide(); 
} 

另一種選擇是重新拋出異常顯示消息後,並有調用代碼處理它在嘗試捕捉:

public void TryMe() 
{ 
try 
{ 
    SomeMethod(); 
    } 
    catch (Exception exception) 
    { 
    // log exception? 
    throw; 
    } 
    } 

調用代碼:

try 
    { 
    CActions.TryMe(); 
    this.Hide(); 
    } 
    catch (Exception ex) 
    { 
     // error handling 
    } 
+0

謝謝!這是一種很好的編碼習慣嗎?我是C#的新手,如果我的整個錯誤處理模型思路錯誤,現在就是改變它的最佳時刻。:) – 2013-05-14 07:08:22

+0

有許多選項 - 您可以從TryMe重新拋出錯誤(將Throw()放入catch塊中)並讓調用代碼也處理錯誤 – NDJ 2013-05-14 07:10:18

+0

當然,這是一個選項。但哪一個是最好的?我的意思是準確的工作,乾淨和可讀的代碼之間的平衡? – 2013-05-14 07:12:39

2

另一種選擇是委派控制流呼叫者,所以:

public void TryMe() 
{ 
    try 
    { 
     SomeMethod(); 
    } 
    catch(Exception exception){ 
     throw; 
    } 
} 

,並使用它像

Actions CAactions = new Actions(); 
try { 
    CActions.TryMe(); 
    //continue, all ok. 
} 
catch(Excepiton ex) { 
     //hide a form, exception happens inside a method 
} 
+0

謝謝!但是我想知道在表單類中進行try-catch是否是一個好習慣? – 2013-05-14 07:13:35

+0

@PaulReed:當然,這是做到這一點的方法之一。 Form類是你的調用者,所以它知道如果某個調用失敗或成功會發生什麼。 – Tigran 2013-05-14 07:16:33

+2

'拋出異常;'會覆蓋原始異常的調用堆棧。它的目的是?通常最好只使用'throw;' – Artemix 2013-05-14 08:29:33

1

爲你的代碼州,Exception被捕獲,並且它的Message財產傳遞給MessageBox。這意味着,絕不會讓你的代碼被打斷,或者讓你有機會冒泡。

附註:在類try/catch(或任何其他方法)中顯示MessageBox被認爲是一種不好的做法。原因很明顯:它使你的類依賴於在圖形應用程序環境中使用,並且違背了類的可重用性。最好沿着任何類型的應用程序可以處理的方法返回類型傳播Exception,例如,包含Message和/或InnerException文本的字符串。

然後你可以做例如

string methodResult = myObject.MyMethod(); 
if(String.IsNullOrEmpty(myMethodResult)) //... everything worked out ok 
... 
else //well then at least you have an error message to work with 
2

你應該避免調用MessageBox.Show()任何地方,但你的應用程序(例如,您的形式)的UI端。這被認爲是不好的做法。所以我會修改NDJ的答案:

public bool TryMe() 
{ 
    try 
    { 
     SomeMethod(); 
     return true; 
    } 
    catch (Exception exception) 
    { 
     //insert some logging here, if YOU need the callstack of your exception 
     return false; 
    } 
} 

if (CActions.TryMe()) 
{ 
    this.Hide(); 
} 
else 
{ 
    MessageBox.Show(...); //insert some meaningful message, useful to END-USER here, not some "Null refrence exception!!11" message, which no one but you will understand 
}