【发布时间】:2023-03-20 12:05:01
【问题描述】:
我将这部分代码从 vb 翻译成 c# 并给了我这个错误信息。 “并非所有代码路径都返回值”。问题是什么?提前致谢。
public DataSet LoadSearchDataSet(string strConnection, string strSQL)
{
//The purpose of this function is to create and populate a data
//set based on a SQL statement passed in to the function.
try
{
DataSet dsData = new DataSet();
//call the table in the local dataset "results" since the values
//may be coming from multiple tables.
string strTableName = "Results";
bool blnRunStoredProc = false;
dsData = PopulateDataSetTable(strConnection, strTableName, strSQL, blnRunStoredProc, dsData);
WriteSampleDataToOutputWindow(dsData);
//return the data set to the calling procedure
return dsData;
}
catch
{
//error handling goes here
UnhandledExceptionHandler();
}
}
【问题讨论】:
-
问题正如编译器所说的那样 - 并非所有代码路径都返回一个值。如果你捕捉到一个异常,你期望会发生什么?你打电话给
UnhandledExceptionHandler,但是你期望得到什么? -
你的catch语句也必须返回一个值!或者将返回值放在try catch之外
-
你需要在那个捕获中投掷(或某种形式的回报)
标签: c#