相关疑难解决方法(0)

编写重试逻辑最干净的方法?

偶尔我需要在放弃之前多次重试一次手术.我的代码是这样的:

int retries = 3;
while(true) {
  try {
    DoSomething();
    break; // success!
  } catch {
    if(--retries == 0) throw;
    else Thread.Sleep(1000);
  }
}
Run Code Online (Sandbox Code Playgroud)

我想在一般的重试函数中重写它,如:

TryThreeTimes(DoSomething);
Run Code Online (Sandbox Code Playgroud)

在C#中有可能吗?该TryThreeTimes()方法的代码是什么?

.net c#

434
推荐指数
13
解决办法
20万
查看次数

使用goto有什么问题?

可能重复:
为什么使用goto不好?
GOTO仍被视为有害吗?

我通过xkcd进行了调整,看到了这个(几年前还读了一些关于它们的负面文章):
你的慢连接很糟糕,得到一个更快的看到这个图像
这究竟是什么问题?为什么goto甚至可以在C++中使用呢?

我为什么使用它们?

c++ goto

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

如何改进此异常重试方案?

我有一个我正在调用的Web服务方法,它是第三方和我的域之外.由于某种原因,Web服务偶尔会出现网关超时.它的间歇性和在尝试失败后直接调用它可以成功.

现在我有一个编码困境,我有代码应该做的伎俩,但代码看起来像业余时间,你会在下面看到.

这是非常糟糕的代码,还是可以接受的?如果不能接受,我该如何改进呢?

在看的同时请努力保持笔直.

try
{
    MDO = OperationsWebService.MessageDownload(MI);
}
catch
{
    try
    {
        MDO = OperationsWebService.MessageDownload(MI);
    }
    catch
    {
        try
        {
            MDO = OperationsWebService.MessageDownload(MI);
        }
        catch
        {
            try
            {
                MDO = OperationsWebService.MessageDownload(MI);
            }
            catch 
            {
                try
                {
                    MDO = OperationsWebService.MessageDownload(MI);
                }
                catch (Exception ex)
                {
                    // 5 retries, ok now log and deal with the error.
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# exception-handling

22
推荐指数
3
解决办法
9404
查看次数

在没有goto的情况下编写重试逻辑的更好方法

有没有更好的方法来编写这段代码而不使用goto?这看起来很尴尬,但我想不出更好的方法.我需要能够执行一次重试尝试,但我不想复制任何代码.

public void Write(string body)
{
    bool retry = false;
RetryPoint:
    try
    {
        m_Outputfile.Write(body);
        m_Outputfile.Flush();
    }
    catch (Exception)
    {
        if( retry )
            throw; 
        // try to re-open the file...
        m_Outputfile = new StreamWriter(m_Filepath, true);
        retry = true;
        goto RetryPoint;
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# goto

7
推荐指数
3
解决办法
6334
查看次数

Try-Catch和"继续" - 这可能吗?

我的代码中有一个部分,我在网络上查询所有SQL Server数据库.我首先尝试使用SQL登录来访问SQL Server实例,但如果失败,那么我想尝试使用我的Windows凭据进行连接.之后,如果我仍然无法连接,那么我希望代码失败,然后通知用户.

所以我想我要问的是如何从Try-Catch块内部循环回到Try-Catch块上方的行:

String conxString = @"Data Source=SQLInstance1;User ID=FOO;Password=BAR;";
bool secondTime = false;

using (SqlConnection sqlConx = new SqlConnection(conxString))
     {
         Try{
               sqlConx.Open();
               DataTable tblDatabases = sqlConx.GetSchema("Databases");
               sqlConx.Close();
               secondTime = false;
               Console.WriteLine("SQL Server found!");
         }
         Catch(System.Data.SqlClient.SqlException e){
                if (!secondTime){
                   secondTime = true;
                   conxString = @"Data Source=SQLInstance1; Integrated Security=True;";
                      //Loop back to the using statement to try again with Windows Creds
                {
                 else{
                   Console.WriteLine("SQL Server not found or credentials refused");
                 }
                   //Report Failure to connect to user

         }
         finally{
            //Reset Variable …
Run Code Online (Sandbox Code Playgroud)

.net c# try-catch

5
推荐指数
2
解决办法
7847
查看次数

尝试/捕获异常从导致异常的行继续

抛出异常时,如何捕获它然后从导致错误的行开始继续执行?

编辑:我们的程序与Indesign Server进行通信,它一直崩溃并抛出随机的COM相关错误(这些错误与服务器本身的错误有关).Indesign Server也需要很长时间来处理命令,因此当它崩溃时,我们希望避免重新启动执行.相反,我们希望从导致异常的行继续.程序中的任何行都可能导致异常.所以从技术上讲,我们不能使用循环.

c#

3
推荐指数
1
解决办法
3973
查看次数

标签 统计

c# ×5

.net ×4

goto ×2

c++ ×1

exception-handling ×1

try-catch ×1