2013-02-28 66 views
0

我在类中声明的数据集公开这是代码数据套装 - 不是所有的代码路径返回

public DataSet GetUsersDataSet() 
    { 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlDataAdapter da = new SqlDataAdapter("SELCT * FROM login", con); 
     DataSet ds = new DataSet(); 
     try 
     { 
      con.Open(); 
      da.Fill(ds, "login"); 
      return ds; 

     } 

     catch 
     { 
      new ApplicationException("Data Error:"); 

     } 
     finally { con.Close(); } 


    } 

但它给我的错误值:

WebApplication1.classes.LoginDB.GetUsersDataSet()': not all code paths return a value.

但在我的代码ds正在返回,我试过在删除try try的时候就试过了。任何人都可以告诉什么是错误。?

+0

你的回报必须尝试之外,赶上 – Zaki 2013-02-28 10:10:41

+0

只需添加在你的catch块 – 2013-02-28 10:12:04

+1

回报有时候开发应该忘记,他们是人类。如果您是编译器,那么当'try'块中发生异常时,如何决定要返回什么?顺便说一句,你需要抛出'ApplicationException'。不只是创建一个实例 – Leri 2013-02-28 10:14:04

回答

1

如果try失败会发生什么情况?您需要try/catch区块以外的return声明。

public DataSet GetUsersDataSet() 
{ 
    SqlConnection con = new SqlConnection(connectionString); 
    SqlDataAdapter da = new SqlDataAdapter("SELCT * FROM login", con); 
    DataSet ds = new DataSet(); 

    try 
    { 
     con.Open(); 
     da.Fill(ds, "login"); 
    } 
    catch 
    { 
     new ApplicationException("Data Error:"); 
    } 
    finally { con.Close(); } 

    return ds; 
} 
+0

现在好了,谢谢 – Rakesh 2013-02-28 10:16:13

0

因为你的方法的返回类型是DataSet,你也应该返回try/catch声明之外,因为你try块可能会失败。如果失败,您的方法不会返回任何值。这就是你得到这个错误的原因。你可以像使用它一样;

public DataSet GetUsersDataSet() 
{ 
    SqlConnection con = new SqlConnection(connectionString); 
    SqlDataAdapter da = new SqlDataAdapter("SELCT * FROM login", con); 
    DataSet ds = new DataSet(); 
    try 
    { 
     con.Open(); 
     da.Fill(ds, "login"); 

    } 

    catch 
    { 
     new ApplicationException("Data Error:"); 

    } 
    finally { con.Close(); } 

    return ds; 

} 
0

想象一下,如果你有一个异常,你不会在catch块中返回任何东西。所以 添加try catch块的返回结束。使用单个回报而不是多个回报。

try 
{ 
    con.Open();  
    da.Fill(ds, "login"); 

} 
catch 
{ 
    new ApplicationException("Data Error:"); 

} 
return ds; 
0

你的方法应该返回一个dataset .The返回的数据集应该是外try-catch语句..

public DataSet GetUsersDataSet() 
{ 
    SqlConnection con = new SqlConnection(connectionString); 
    SqlDataAdapter da = new SqlDataAdapter("SELCT * FROM login", con); 
    DataSet ds = new DataSet(); 
    try 
    { 
     con.Open(); 
     da.Fill(ds, "login"); 


    } 

    catch 
    { 
     new ApplicationException("Data Error:"); 

    } 
    finally { con.Close(); } 

    return ds; 

} 
0

这是可能的,如果da.Fill抛出异常的方法不return一个DataSet(FE ),但这是return声明的合同。

public DataSet GetUsersDataSet() 
{ 
    DataSet ds = new DataSet(); 
    // ... 
    try{ 
     // ... 
    } 
    catch{ 
     //... 
    } 
    finally{ 
     //... 
    } 
    return ds; 
} 

C#规格:

8.9.4 The return statement returns control to the caller of the function member in which the return statement appears.

// ...

If the return statement is enclosed by one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all enclosing try statements have been executed.

0

你根本都忘了

您可以通过移动returntry-catch以外确保其抛出你在catch上创建的异常。这应该能解决你的问题。

看看这个link

0

据我了解,你想在数据库操作失败,重新抛出异常:

public DataSet GetUsersDataSet() 
{ 
    SqlConnection con = new SqlConnection(connectionString); 
    SqlDataAdapter da = new SqlDataAdapter("SELCT * FROM login", con); 
    DataSet ds = new DataSet(); 
    try 
    { 
     con.Open(); 
     da.Fill(ds, "login"); 
     return ds; 

    } 
    catch 
    { 
     throw new ApplicationException("Data Error:"); 

    } 
    finally { con.Close(); } 
} 

所以,如果异常将在try块抛出ApplicationException将重新在finally块关闭连接后抛出。

如果我错了,这不是你想要的。 new ApplicationException("Data Error:");不会在您的catch区块中执行任何操作,但会创建ApplicationException的新实例,因此您应该将其删除。

+0

那么更好的办法是什么?这是正确的catch(Exception ex){lblError.text = ex.message.toString();}或catch中的自定义消息。 – Rakesh 2013-02-28 10:28:50

+0

@Vijesh你不应该捕捉所有的异常并将它们静音。如果你必须捕捉所有异常(即你的例子),你应该总是重新抛出它们。当你重新抛出不同的异常时,总是把捕获到的异常添加为'InnerException'。同样,返回空数据集也没什么问题,直到您想要精确显示发生的事情。 – Leri 2013-02-28 10:33:03