2016-05-06 62 views
0

我得到的是從數據庫中,它看起來像或多或少這樣的填充數據集的方法:的WinForms異常處理

private DataSet GetData(string query) 
{ 
    try 
    { 
     //do some stuff to populate dataset 
     return dataset; 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     LogExceptionToFile(ex); //to log whole exception stack trace, etc. 
    } 
    finally 
    { 
     //cleanup 
    } 
} 

//calling methods: 
GetData(query); 
OtherMethod1(); //this method shows message box of success 

當我有一段代碼在例外的情況下,我得到了我的用戶友好的消息框然後調用OtherMethod1()並顯示成功消息框。如果在GetData()有錯誤,我想停止。當我在消息框後添加throw;到此catch塊時,顯示的另一個消息框比拋出未處理的異常。如果我提供了友好的信息,我想避免顯示第二個消息框。

+0

如果您在'GetData()'方法中添加'throw',則需要圍繞該方法進行try/catch以實際捕獲異常。否則,您需要一些變量來跟蹤GetData中的成功/失敗,然後根據是否存在異常來採取相應措施。 –

回答

2

可以返回一個值,表明成功:

private bool TryGetData(string query, out DataSet dataSet) 
{ 
    try 
    { 
     dataSet = ...; 
     //do some stuff to populate dataset 
     return true; 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     LogExceptionToFile(ex); //to log whole exception stack trace, etc. 
     return false; 
    } 
    finally 
    { 
     //cleanup 
    } 
} 

//calling methods: 
DataSet ds; 
if (TryGetData(query, out ds)) 
{ 
    OtherMethod1(); 
} 
else 
{ 
    //error 
} 
0

我要說的是,你可以使用一個回調。

Here's a link on returning multiple values

讓您GetData()方法使用回調返回多個值。然後您可以設置,比如GetData()方法給出的callBack的值爲success bool。

然後繼續在您的呼叫代碼中,只有運行OtherMethod()如果success布爾值爲true

您甚至可以將ex異常作爲回調的一部分返回,並使用一段代碼顯示一個對話框;不管它是成功還是失敗,如果是這樣,並且在其中顯示異常。

實施例:

private DataSet GetData(string query, Action<bool> callBack) 
{ 
    bool successful = false; // This will be returned in the callback. 
    DataSet returnValue; // This will be the dataset stuff. 
    try 
    { 
     //do some stuff to populate dataset 
     returnValue = ???; // Populate your return value , but don't return yet; 
     successful = true; // This will indicate success. 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     LogExceptionToFile(ex); //to log whole exception stack trace, etc. 
    } 
    finally 
    { 
     //cleanup 

     if (callBack != null) // Send the callback with the boolean of successful. 
     { 
      callBack(successful); 
     } 

     return returnValue; // Now return your DataSet. 
    } 
} 

bool success = false; // Use this to determine if GetData was successful. 
//calling methods: 
GetData(query, (s) => success = s); 

if (success) 
{ 
    OtherMethod1(); //this method shows message box of success 
} 
else 
{ 
    // Do something else, or show your failure message box. 
} 

更清潔,這種方式。

希望這會有所幫助!

1

如果我正確理解你的困境,你可以重新拋出和處理,像這樣的(目前未處理的)異常:

private DataSet GetData(string query) 
{ 
    try 
    { 
     //do some stuff to populate dataset 
     return dataset; 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     LogExceptionToFile(ex); //to log whole exception stack trace, etc. 

     throw; 
    } 
    finally 
    { 
     //cleanup 
    } 
} 


//calling methods: 
try 
{ 
    GetData(query); 
    OtherMethod1(); //this method shows message box of success 
} 
catch(Exception ex) 
{ 
    //Do whatever you need to do here, if anything. 
} 

現在,這肯定不是這樣做的唯一途徑,我m只向你展示如何去做你聽到的事情。其他一些答案也很好,可能更適合您的特定情況。

0

如果發生錯誤,您可以簡單地返回null。 (雖然,使用的null可能取決於你說話或在您的公司或項目的編碼規則可能是什麼誰氣餒)

private DataSet GetData(string query) 
{ 
    try 
    { 
     //do some stuff to populate dataset 
     return dataset; 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     LogExceptionToFile(ex); //to log whole exception stack trace, etc. 
    } 
    finally 
    { 
     //cleanup 
    } 

    return null; 
} 

//calling methods: 
var result = GetData(query); 
if (result != null) 
    OtherMethod1(); //this method shows message box of success 
0

可以在GetData()添加throw;catch

private DataSet GetData(string query) 
{ 
    try 
    { 
     //do some stuff to populate dataset 
     return dataset; 
    } 
    catch (SqlException ex) 
    { 
     MessageBox.Show("There was a database error. Please contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     LogExceptionToFile(ex); //to log whole exception stack trace, etc. 
     throw; 
    } 
    finally 
    { 
     //cleanup 
    } 
} 

,然後做到這一點:

try 
{ 
    GetData(query); 
    OtherMethod1(); 
} 
catch (Exception ex) 
{ 
    // do something to ex if needed 
} 

這樣你就不會得到第二個消息框和CAN H如果需要的話再次發生異常。