C# WinForms异常处理
我得到了一个从db填充数据集的方法,它大致如下所示:C# WinForms异常处理,c#,error-handling,exception-handling,C#,Error Handling,Exception Handling,我得到了一个从db填充数据集的方法,它大致如下所示: 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 c
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()
中出现错误,我想停止。当我添加抛出时
在我的消息框之后,显示了另一个消息框,该消息框引发了未处理的异常。如果提供了友好消息,我希望避免显示第二个消息框。您可以返回一个表示成功的值:
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
}
您可以返回一个指示成功的值:
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
}
我想你可以用回拨电话 让您的
GetData()
方法使用回调返回多个值。然后,您可以从GetData()
方法给出的callBack
值中设置success
bool
然后,在调用代码中,仅当success
bool为true时,才运行OtherMethod()
您甚至可以将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.
}
私有数据集GetData(字符串查询、操作回调)
{
bool successful=false;//这将在回调中返回。
DataSet returnValue;//这将是数据集内容。
尝试
{
//做一些事情来填充数据集
returnValue=???;//填充您的返回值,但不要返回;
successful=true;//这表示成功。
}
catch(SqlException-ex)
{
MessageBox.Show(“出现数据库错误,请与管理员联系。”,“错误”,MessageBoxButtons.OK,MessageBoxIcon.error);
LogExceptionFile(ex);//记录整个异常堆栈跟踪等。
}
最后
{
//清理
if(callBack!=null)//发送布尔值为successful的回调。
{
回调(成功);
}
returnValue;//现在返回数据集。
}
}
bool success=false;//使用此选项确定GetData是否成功。
//调用方法:
GetData(查询(s)=>success=s);
如果(成功)
{
OtherMethod1();//此方法显示成功的消息框
}
其他的
{
//执行其他操作,或显示失败消息框。
}
那样就干净多了
希望这有帮助 我想说您可以使用回调 让您的
GetData()
方法使用回调返回多个值。然后,您可以从GetData()
方法给出的callBack
值中设置success
bool
然后,在调用代码中,仅当success
bool为true时,才运行OtherMethod()
您甚至可以将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.
}
私有数据集GetData(字符串查询、操作回调)
{
bool successful=false;//这将在回调中返回。
DataSet returnValue;//这将是数据集内容。
尝试
{
//做一些事情来填充数据集
returnValue=???;//填充您的返回值,但不要返回;
successful=true;//这表示成功。
}
catch(SqlException-ex)
{
MessageBox.Show(“出现数据库错误,请与管理员联系。”,“错误”,MessageBoxButtons.OK,MessageBoxIcon.error);
LogExceptionFile(ex);//记录整个异常堆栈跟踪等。
}
最后
{
//清理
if(callBack!=null)//发送布尔值为successful的回调。
{
回调(成功);
}
returnValue;//现在返回数据集。
}
}
bool success=false;//使用此选项确定GetData是否成功。
//调用方法:
GetData(查询(s)=>success=s);
如果(成功)
{
OtherMethod1();//此方法显示成功的消息框
}
其他的
{
//执行其他操作,或显示失败消息框。
}
那样就干净多了
希望这有帮助 如果我正确理解了您的困境,您可以重新浏览并处理(当前未处理的)异常,如下所示:
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.
}
现在,这当然不是唯一的方法,我只是告诉你怎么做,听起来像你在尝试。其他一些答案也很好,可能更适合您的特殊情况。如果我正确理解您的困境,您可以重新浏览并处理(当前未处理的)异常,如下所示:
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.
}
现在,这当然不是唯一的方法,我只是告诉你怎么做,听起来像你在尝试。其他一些答案也很好,可能更适合您的具体情况。如果出现错误,您只需返回
null
。(尽管可能不鼓励使用null
,这取决于您与谁交谈,或者您公司或项目中的编码准则。)
如果出现错误,只需返回
null
。(尽管可能不鼓励使用null
,这取决于您与谁交谈,或者您公司或项目中的编码准则。)
您可以添加
throw
到GetData()中的catch
:
然后这样做:
try
{
GetData(query);
OtherMethod1();
}
catch (Exception ex)
{
// do something to ex if needed
}
这样,您就不会得到第二个消息框,并且可以在需要时再次处理异常。您可以添加throw
到GetData()中的catch
:
然后这样做:
try
{
GetData(query);
OtherMethod1();
}
catch (Exception ex)
{
// do something to ex if needed
}
这样你就不会得到第二个m