我再一次进行了设计评审,并且遇到了一个声称特定情景的概率"低于宇宙射线的风险"影响该程序的说法,并且我发现我没有最微弱的想法是什么概率是.
"因为2 -128是340282366920938463463374607431768211456中的1个,我认为我们在这里抓住机会是合理的,即使这些计算已经减少了几十亿......我们对宇宙射线的风险更大我相信,把我们搞砸了."
这个程序员是否正确?宇宙射线撞击计算机并影响程序执行的概率是多少?
statistics physics probability error-detection risk-analysis
我正在为一位朋友审查一些代码,并说他在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超越finally块.有谁知道这些条件是什么?
语法将从语言变为语言,但这是一个普遍的问题.
这有什么区别....
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch (NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
finally
{
Console.WriteLine("Executing finally block.");
}
Run Code Online (Sandbox Code Playgroud)
还有这个....
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch (NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
Console.WriteLine("Executing finally block.");
Run Code Online (Sandbox Code Playgroud)
我一直看到它被使用,所以我认为有一个很好的理由最终使用,但我无法弄清楚它是如何只是在声明之后放置代码,因为它仍然会运行.
有没有最终没有运行的场景?
在Try Catch Finally块中,finally块总是执行,无论是什么,或者只有catch块没有返回错误?
我的印象是,如果catch块没有错误,则finally块只会执行.如果由于错误而执行了catch块,那么它是否应该一起停止执行并返回错误消息(如果有的话)?
以下面的代码为例:
try
{
Response.Redirect(someurl);
}
finally
{
// Will this code run?
}
Run Code Online (Sandbox Code Playgroud)
finally块中的代码会运行吗?
另一个面试问题是期待一个真/假答案,我不太确定.
出于好奇,我想知道是否有可能出现垃圾收集器无法运行或根本不运行的情况(可能是由于异常)?
如果是,很可能会出现OutOfMemory/Stackoverflow异常.那么在这种情况下,仅通过查看异常消息,stacktrace等,我们就可以确定gc未能运行的核心问题.
我有一小段代码调用webservice客户端方法.
该方法返回一个消息数组作为out参数.这些消息通常包含已发生的任何错误的详细信息.发生错误时,也会引发异常.
我想记录消息,无论是抛出异常还是异常类型.登录finally块是否可以接受?
WebServiceClient client = GetWebServiceClient();
Console.WriteLine("Calling getUpdates...");
ItemStatus[] itemStatuses;
Message[] messages = null;
string outToken;
try
{
outToken = client.getUpdates(inToken, out itemStatuses, out messages);
}
finally
{
LogMessages(messages);
}
Run Code Online (Sandbox Code Playgroud) 请注意以下代码:
class CTestFinally
{
public static void Run()
{
try
{
TryAndTry();
}
catch (Exception exError)
{
Console.WriteLine(exError.Message);
}
finally
{
Console.WriteLine("Finally...!");
}
Console.ReadKey();
}
static void TryAndTry()
{
try
{
TryAndTry();
}
catch (Exception exError)
{
Console.WriteLine(exError.Message);
}
finally
{
Console.WriteLine("Try: Finally...!");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后从未执行因为我们得到堆栈溢出错误.
除了上述问题之外,还有哪种情况下finally块不会被执行?
在此链接(https://docs.python.org/2/tutorial/errors.html#defining-clean-up-actions)下面说:
在离开try语句之前总是执行finally子句,无论是否发生了异常.
代码1:
try:
print "Performing an action which may throw an exception."
except Exception, error:
print "An exception was thrown!"
print str(error)
else:
print "Everything looks great!"
finally:
print "Finally is called directly after executing the try statement whether an exception is thrown or not."
Run Code Online (Sandbox Code Playgroud)
输出1:
Performing an action which may throw an exception.
Everything looks great!
Finally is called directly after executing the try statement whether an exception is thrown or not.
Run Code Online (Sandbox Code Playgroud)
代码2:
try:
print "Performing …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×4
exception ×2
finally ×2
asp.net ×1
javascript ×1
logging ×1
physics ×1
probability ×1
python ×1
statistics ×1
try-catch ×1