相关疑难解决方法(0)

从try catch finally块中返回是不好的做法吗?

所以今天早上我遇到了一些看起来像这样的代码:

try
{
    x = SomeThingDangerous();
    return x;
}
catch (Exception ex)
{
    throw new DangerousException(ex);
}
finally
{
    CleanUpDangerousStuff();
}
Run Code Online (Sandbox Code Playgroud)

现在这段代码编译得很好并且可以正常工作,但是从try块中返回它感觉不对,特别是如果最终有关联的话.

我的主要问题是如果最终抛出它自己的例外会发生什么?你有一个返回的变量,但也有一个例外来处理...所以我有兴趣知道其他人在try块中返回的想法?

c# try-catch try-catch-finally

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

如果从catch块中抛出异常,最终何时运行?

try {
   // Do stuff
}
catch (Exception e) {
   throw;
}
finally {
   // Clean up
}
Run Code Online (Sandbox Code Playgroud)

在上面的块中,finally块是什么时候调用的?在抛出e之前或者最后被召唤然后赶上?

c#

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

WCF超时异常详细调查

我们有一个应用程序,它具有在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)

wcf timeout timeoutexception

94
推荐指数
1
解决办法
11万
查看次数

C#"最终"阻止总是执行吗?

可能重复:
如果我在Try块中返回一个值,是否会触发Finally语句中的代码?

请考虑以下代码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中自己测试过.但是,请随时回答!

c# try-catch

68
推荐指数
5
解决办法
8万
查看次数

在try/catch/finally中"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)

我一直看到它被使用,所以我认为有一个很好的理由最终使用,但我无法弄清楚它是如何只是在声明之后放置代码,因为它仍然会运行.

有没有最终没有运行的场景?

javascript c# exception-handling finally

21
推荐指数
3
解决办法
5109
查看次数

在Windows服务中运行的线程中最终不会执行

任何人都可以解释为什么这个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)

输出 …

c# windows-services timer try-catch-finally

16
推荐指数
1
解决办法
1666
查看次数

C# - Try-Catch-Finally on Return

我有以下代码:

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 ?

谢谢

c# return try-catch

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


是否存在finally块未执行的任何情况?

请注意以下代码:

 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块不会被执行?

c#

2
推荐指数
1
解决办法
223
查看次数

finally块中的语句

可能重复:
尝试抓住最后的问题

如果未捕获异常,则执行代码语句

try
{
  throw new Exception("test example");    
}

finally
{
  Console.WriteLine("finally block"); 
}
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
95
查看次数

c#中return关键字和finally关键字之间的关系

我想找出"return"和"finally"关键字之间的关系.什么是执行顺序以及发生异常时会发生什么,并且在代码块执行某些操作后调用return关键字,如果有两个嵌套的finally块,如下所示,

        try
        {
            try
            {

            }
            catch (Exception)
            {
                //do some stuff
                return;
            }
            finally
            {

            }
        }
        catch (Exception)
        {

        }
        finally
        {

        }
Run Code Online (Sandbox Code Playgroud)

c# exception-handling

0
推荐指数
1
解决办法
164
查看次数

返回后会终于运行吗?

我有这段代码:

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块会关闭我的连接吗?

这样安全吗?

c# exception-handling return try-catch

-2
推荐指数
2
解决办法
2030
查看次数