所以今天早上我遇到了一些看起来像这样的代码:
try
{
x = SomeThingDangerous();
return x;
}
catch (Exception ex)
{
throw new DangerousException(ex);
}
finally
{
CleanUpDangerousStuff();
}
Run Code Online (Sandbox Code Playgroud)
现在这段代码编译得很好并且可以正常工作,但是从try块中返回它感觉不对,特别是如果最终有关联的话.
我的主要问题是如果最终抛出它自己的例外会发生什么?你有一个返回的变量,但也有一个例外来处理...所以我有兴趣知道其他人在try块中返回的想法?
try {
// Do stuff
}
catch (Exception e) {
throw;
}
finally {
// Clean up
}
Run Code Online (Sandbox Code Playgroud)
在上面的块中,finally块是什么时候调用的?在抛出e之前或者最后被召唤然后赶上?
我们有一个应用程序,它具有在IIS7上运行的WCF服务(*.svc)以及查询该服务的各种客户端.服务器正在运行Win 2008 Server.客户端正在运行Windows 2008 Server或Windows 2003服务器.我得到以下异常,我看到它实际上可能与大量潜在的WCF问题有关.
System.TimeoutException: The request channel timed out while waiting for a reply after 00:00:59.9320000. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout. ---> System.TimeoutException: The HTTP request to 'http://www.domain.com/WebServices/myservice.svc/gzip' has exceeded the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码C#代码."finally"块是否执行?
public void DoesThisExecute() {
string ext = "xlsx";
string message = string.Empty;
try {
switch (ext) {
case "xls": message = "Great choice!"; break;
case "csv": message = "Better choice!"; break;
case "exe": message = "Do not try to break me!"; break;
default:
message = "You will not win!";
return;
}
}
catch (Exception) {
// Handle an exception.
}
finally {
MessageBox.Show(message);
}
}
Run Code Online (Sandbox Code Playgroud)
哈,在我写完这篇文章之后,我意识到我本可以在Visual Studio中自己测试过.但是,请随时回答!
语法将从语言变为语言,但这是一个普遍的问题.
这有什么区别....
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)
我一直看到它被使用,所以我认为有一个很好的理由最终使用,但我无法弄清楚它是如何只是在声明之后放置代码,因为它仍然会运行.
有没有最终没有运行的场景?
任何人都可以解释为什么这个finally块没有被执行?我已经阅读了关于何时期望最终阻止不被执行的帖子,但这似乎是另一种情况.此代码需要TopShelf和log4net.我正在运行.net 4.5
我想它必须是Windows服务引擎启动未处理的异常,但为什么它在finally块完成之前运行?
using log4net;
using log4net.Config;
using System;
using System.Threading;
using Topshelf;
namespace ConsoleApplication1
{
public class HostMain
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.Service<HostMain>(s =>
{
s.ConstructUsing(name => new HostMain());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetServiceName("TimerTest");
});
}
public void Stop()
{
LogManager.GetLogger("MyLog").Info("stopping");
}
public void Start()
{
XmlConfigurator.Configure();
LogManager.GetLogger("MyLog").Info("starting");
new Thread(StartServiceCode).Start();
}
public void StartServiceCode()
{
try
{
LogManager.GetLogger("MyLog").Info("throwing");
throw new ApplicationException();
}
finally
{
LogManager.GetLogger("MyLog").Info("finally");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出 …
我有以下代码:
public DataTable GetAllActiveUsers()
{
DataTable dataTable = new DataTable();
try
{
connection.Open();
SqlCommand getAllActiveUsersCommand = new SqlCommand(getAllUsers, connection);
SqlDataAdapter dataAdapter = new SqlDataAdapter(getAllActiveUsersCommand);
dataAdapter.Fill(dataTable);
return dataTable;
}
catch(Exception e)
{
Console.WriteLine(e);
return null;
}
finally
{
connection.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
这基本上是我的数据库中的活跃用户.但有人可以向我解释,Finally如果成功运行try块并返回DataTable,是否会执行Block ?
谢谢
另一个面试问题是期待一个真/假答案,我不太确定.
请注意以下代码:
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块不会被执行?
可能重复:
尝试抓住最后的问题
如果未捕获异常,则执行代码语句
try
{
throw new Exception("test example");
}
finally
{
Console.WriteLine("finally block");
}
Run Code Online (Sandbox Code Playgroud) 我想找出"return"和"finally"关键字之间的关系.什么是执行顺序以及发生异常时会发生什么,并且在代码块执行某些操作后调用return关键字,如果有两个嵌套的finally块,如下所示,
try
{
try
{
}
catch (Exception)
{
//do some stuff
return;
}
finally
{
}
}
catch (Exception)
{
}
finally
{
}
Run Code Online (Sandbox Code Playgroud) 我有这段代码:
try{
this.connection.Open();
cmd.ExecuteScalar();
return true;
}
catch(Exception exc){
throw exc;
}
finally{
this.connection.Close();
}
Run Code Online (Sandbox Code Playgroud)
我知道如果catch抛出异常,该finally块将继续运行.
但是回归try呢?
如果try块返回true,finally块会关闭我的连接吗?
这样安全吗?