C# error “Not all code paths return a value”

后端 未结 8 1860
长发绾君心
长发绾君心 2020-12-12 06:05

I translated this part of the code from vb to c# and giving me this error message. \"Not all code paths return a value\". What is the problem? Thanks in advance.

<         


        
相关标签:
8条回答
  • 2020-12-12 06:12

    It is must to return proper value in any case. so try to maintain try catch block with return value or outside of try/catch block if nothing to return in try / catch block.

    0 讨论(0)
  • 2020-12-12 06:22

    This is a common error message in functions, as functions are designed to return some value. If your code passes the catch section, it will reach the end of the function without returning anything, thats where you need to return the value.

    0 讨论(0)
  • rewrite like this:

        DataSet dsData = null;
        try
                {
                    //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);
                }
                catch
                {
                    //error handling goes here
                    UnhandledExceptionHandler();
                }
    //return the data set to the calling procedure
                    return dsData;
    
    0 讨论(0)
  • 2020-12-12 06:26

    You are missing the return value in the case the code throws an exception.

    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.
    
        DataSet dsData = new DataSet();
    
        try
        {
            //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);
        }
        catch
        {
            //error handling goes here
            UnhandledExceptionHandler();
        }
    
         //return the data set to the calling procedure
         return dsData;
    }
    
    0 讨论(0)
  • 2020-12-12 06:31

    This is because in the case of any exception occurs,the exception will thrown to the catch, in that case the code will not return any value. so you have to return some value from the catch to avoid this issue

    Replace the catch with this:

            catch
            {
                //error handling goes here
                UnhandledExceptionHandler();
                return new DataSet();
            }
    
    0 讨论(0)
  • 2020-12-12 06:34

    You need to add a return statement after your catch clause!

    In case of an exception inside your try catch clause, you won't return a value. And that's exactly what your error is indicating.

    0 讨论(0)
提交回复
热议问题