C# 无法跳出finally块

C# 无法跳出finally块,c#,try-catch,C#,Try Catch,我试图从函数返回一个值。函数WcfProvider.MetalsPrices可能引发异常。我想避免它 public IEnumerable<PriceOfMetal> GetPrice(int id, DateTime time) { bool condition = false; DateTime timenew = time.AddDays(-1); var allPrice = from c in db.PriceOfMetal

我试图从函数返回一个值。函数
WcfProvider.MetalsPrices
可能引发异常。我想避免它

public IEnumerable<PriceOfMetal> GetPrice(int id, DateTime time)
{
        bool condition = false;
        DateTime timenew = time.AddDays(-1);

        var allPrice = from c in db.PriceOfMetal
                       select c;

        foreach (var i in allPrice)
        {
            if (i.Date.Date == timenew.Date && i.ListOfMetaL_Id==id)
            {
                condition = true;
            }
        }

        try
        {
            if (condition == false)
            {
                var price = WcfProvider.MetalsPrices(id, time, time).Tables[0].AsEnumerable()
                    .Select(
                        a =>
                            new PriceOfMetal()
                            {
                                Date = a.Field<DateTime>("Date"),
                                ListOfMetaL_Id = a.Field<int>("MetalId"),
                                Value = a.Field<System.Double>("Price")
                            })
                    .ToList().Single();

                db.PriceOfMetal.Add(price);
                db.SaveChanges();
            }
        }
        finally 
        {
            var all = from c in db.PriceOfMetal select c;
            return all;
        }
public IEnumerable GetPrice(int-id,DateTime)
{
布尔条件=假;
DateTime timenew=time.AddDays(-1);
var allPrice=以db为单位的c.金属价格
选择c;
foreach(所有价格中的var i)
{
if(i.Date.Date==timenew.Date&&i.ListOfMetaL_Id==Id)
{
条件=真;
}
}
尝试
{
如果(条件==false)
{
var price=WcfProvider.MetalsPrices(id,time,time).Tables[0].AsEnumerable()
.选择(
a=>
新金属价格
{
日期=a.字段(“日期”),
ListOfMetaL_Id=a.Field(“MetalId”),
值=一个字段(“价格”)
})
.ToList().Single();
db.金属价格。添加(价格);
db.SaveChanges();
}
}
最后
{
var all=以db表示的c。金属价格选择c;
全部归还;
}

我想最后返回块的值。有可能吗?我遇到了一个错误。

您可能需要这样的模式

try
{
   return here
}
catch(Exception ex)
{
   // Catch any error
   // re throw if you choose, 
   // or you can return if you choose
   return here
}
finally
{
  // allways do whats here
}
您可能需要阅读以下几页:


再进一步,想象一下,如果我们能在最后一个街区内返回

你可能会有下面这样一段讨厌的代码,充其量也会让人困惑

try
{
    return 10;
}
catch (Exception e)
{
    return 20;
}
finally
{
    return 30;
}

编译器将返回什么?

您可能需要这样的模式

try
{
   return here
}
catch(Exception ex)
{
   // Catch any error
   // re throw if you choose, 
   // or you can return if you choose
   return here
}
finally
{
  // allways do whats here
}
您可能需要阅读以下几页:


再进一步,想象一下,如果我们能在最后一个街区内返回

你可能会有下面这样一段讨厌的代码,充其量也会让人困惑

try
{
    return 10;
}
catch (Exception e)
{
    return 20;
}
finally
{
    return 30;
}

编译器将返回什么?

如果内部发生异常,您必须决定函数是正常返回还是异常返回

如果异常(您的调用者将看到异常):

如果正常,您需要处理异常:

try {
    // do stuff
    return answer;
}
finally {
    // cleanup stuff
}
try {
    // do stuff
}
catch {
    // recover stuff        
}
// cleanup stuff
return answer;

决不能将
return
语句放在
finally
块中,因为
finally
在出现未捕获异常时运行,并且在函数结束时(异常)运行由于未捕获的异常,没有返回值。

如果内部发生异常,您必须决定您的函数应该正常返回还是异常返回

如果异常(您的调用者将看到异常):

如果正常,您需要处理异常:

try {
    // do stuff
    return answer;
}
finally {
    // cleanup stuff
}
try {
    // do stuff
}
catch {
    // recover stuff        
}
// cleanup stuff
return answer;

决不能将
return
语句放在
finally
块中,因为
finally
在出现未捕获异常时运行,并且在函数结束时(异常)运行由于未捕获的异常,没有返回值。

很抱歉,您的问题很模糊,很难回答。您的代码看起来太复杂了。不管怎样,现在是假期。也许下面的内容会帮助您。但不保证

public IEnumerable<PriceOfMetal> GetPrice(int id, DateTime time)
{
    DateTime timenew = time.AddDays(-1);

    var allPrice = from c in db.PriceOfMetal
                   select c;
                   where c.Date.Date == timenew.Date
                   and c.ListOfMetal_Id == id

    if (!allPrice.Any())
    {
        try
        {
            var price = WcfProvider.MetalsPrices(id, time, time).Tables[0].AsEnumerable()
                        .Select(a =>new PriceOfMetal
                        {
                            Date = a.Field<DateTime>("Date"),
                            ListOfMetaL_Id = a.Field<int>("MetalId"),
                            Value = a.Field<System.Double>("Price")
                        })
                        .ToList().Single();

            db.PriceOfMetal.Add(price);
            db.SaveChanges();
        }
        catch
        {
            // Eating exceptions like this is really poor. You should improve the design.
        }
    }
    return db.PriceOfMetal;
}  
public IEnumerable GetPrice(int-id,DateTime)
{
DateTime timenew=time.AddDays(-1);
var allPrice=以db为单位的c.金属价格
选择c;
其中c.Date.Date==timenew.Date
和c.ListOfMetal_Id==Id
如果(!allPrice.Any())
{
尝试
{
var price=WcfProvider.MetalsPrices(id,time,time).Tables[0].AsEnumerable()
.选择(a=>新的金属价格
{
日期=a.字段(“日期”),
ListOfMetaL_Id=a.Field(“MetalId”),
值=一个字段(“价格”)
})
.ToList().Single();
db.金属价格。添加(价格);
db.SaveChanges();
}
抓住
{
//吃这样的异常真的很糟糕。你应该改进设计。
}
}
返回db.priceof金属;
}  

我很抱歉这么说,但你的问题含糊不清,很难回答。你的代码看起来太复杂了。不管怎样,现在是假期。也许下面会帮你。但不能保证

public IEnumerable<PriceOfMetal> GetPrice(int id, DateTime time)
{
    DateTime timenew = time.AddDays(-1);

    var allPrice = from c in db.PriceOfMetal
                   select c;
                   where c.Date.Date == timenew.Date
                   and c.ListOfMetal_Id == id

    if (!allPrice.Any())
    {
        try
        {
            var price = WcfProvider.MetalsPrices(id, time, time).Tables[0].AsEnumerable()
                        .Select(a =>new PriceOfMetal
                        {
                            Date = a.Field<DateTime>("Date"),
                            ListOfMetaL_Id = a.Field<int>("MetalId"),
                            Value = a.Field<System.Double>("Price")
                        })
                        .ToList().Single();

            db.PriceOfMetal.Add(price);
            db.SaveChanges();
        }
        catch
        {
            // Eating exceptions like this is really poor. You should improve the design.
        }
    }
    return db.PriceOfMetal;
}  
public IEnumerable GetPrice(int-id,DateTime)
{
DateTime timenew=time.AddDays(-1);
var allPrice=以db为单位的c.金属价格
选择c;
其中c.Date.Date==timenew.Date
和c.ListOfMetal_Id==Id
如果(!allPrice.Any())
{
尝试
{
var price=WcfProvider.MetalsPrices(id,time,time).Tables[0].AsEnumerable()
.选择(a=>新的金属价格
{
日期=a.字段(“日期”),
ListOfMetaL_Id=a.Field(“MetalId”),
值=一个字段(“价格”)
})
.ToList().Single();
db.金属价格。添加(价格);
db.SaveChanges();
}
抓住
{
//吃这样的异常真的很糟糕。你应该改进设计。
}
}
返回db.priceof金属;
}  

错误说明了什么?错误说明了什么?@Denis bugger,请用更多细节更新您的问题,您的问题或错误说明不太清楚is@Denis臭虫,请用更多的细节更新你的问题,它不太清楚你问什么,或者错误是什么