相关疑难解决方法(0)

尝试中真正发生的事情{return x; } finally {x = null; 声明?

我在另一个问题中看到了这个提示,并想知道是否有人可以向我解释这是如何工作的?

try { return x; } finally { x = null; }
Run Code Online (Sandbox Code Playgroud)

我的意思是,该finally条款真正执行return声明?这段代码的线程不安全吗?你能想到这个try-finally黑客可以做的任何额外的hackery 吗?

.net c# exception-handling

253
推荐指数
4
解决办法
7万
查看次数

如果我在Try块中返回一个值,那么finally语句中的代码会触发吗?

我正在为一位朋友审查一些代码,并说他在try-finally块中使用了一个return语句.即使try块的其余部分没有,Finally节中的代码是否仍会触发?

例:

public bool someMethod()
{
  try
  {
    return true;
    throw new Exception("test"); // doesn't seem to get executed
  }
  finally
  {
    //code in question
  }
}
Run Code Online (Sandbox Code Playgroud)

.net c# exception-handling try-catch

228
推荐指数
7
解决办法
7万
查看次数

在catch区块返回?

在catch块中有一个return语句是错误的吗?有哪些替代方案?
即:

public bool SomeFunction()
{
    try
    {
        //somecode
        return true;
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.message);
        return false;
    }

}
Run Code Online (Sandbox Code Playgroud)

.net c# exception-handling

58
推荐指数
5
解决办法
4万
查看次数

最终回归尝试和捕获与回归?

这些风险中的任何一个都有风险吗?一个更好吗?或者它是你打印出来并投掷飞镖决定的东西之一?

我想这样做,因为我明白了最终的工作方式:

try { 
    stuff that changes something... 
}
catch (System.Exception ex) { 
    something.worked = false; 
    something.err = ex.Message; 
}
finally { 
    stuff.close();
    return something; 
}
Run Code Online (Sandbox Code Playgroud)

但我见过:

try { 
    stuff that changes something...
    return something; 
}
catch (System.Exception ex) { 
    something.worked = false; 
    something.err = ex.Message; 
    return something; 
}
finally { 
    stuff.close(); 
}
Run Code Online (Sandbox Code Playgroud)

c# return finally try-catch

12
推荐指数
2
解决办法
2万
查看次数

try/finally没有catch return语句?

为什么以下代码的结果是3,为什么最终得到终止并退出方法,即使编译器检查首先尝试以及为什么try中的返回不会终止方法?

public int returnVal(){ 
    try{
        return 2;
    }
    finally{
        return 3;
    }
}
Run Code Online (Sandbox Code Playgroud)

java return exception try-catch

6
推荐指数
1
解决办法
616
查看次数

最后返回声明

我有一个ac#program充当客户端和许多客户端程序,它再次是ac#windows应用程序连接到这个c#server程序从sqlite数据库读取数据.为了避免连接多个客户端时出现锁定问题,我使用了以下代码,

System.Threading.Monitor.Enter(Lock);

try                       
{                     
    filter.Execute();//get data from database
    Request.Clear();
    return filter.XML;//create xml and return to client
}
finally
{
    System.Threading.Monitor.Exit(Lock);
}
Run Code Online (Sandbox Code Playgroud)

服务器有时会挂起并需要重新启动服务器程序.在最终之前做出退货声明是一个很好的做法吗?

关心sangeetha

c# return-value

5
推荐指数
1
解决办法
757
查看次数