English 中文(简体)
在Catch栏中进行试验?
原标题:Retries in Catch block?
  • 时间:2011-11-09 09:30:31
  •  标签:
  • c#
  • sql

我如何在捕获区执行该守则?

  try
    {
       // Call a MS SQL stored procedure (MS SQL 2000)
       // Stored Procedure may deadlock 
    }
    catch
    {
       // if deadlocked Call a MS SQL stored procedure (may deadlock again)
       // If deadlocked, keep trying until stored procedure executes
    }
    finally
    {

    }
最佳回答

提出这个建议,并可能在你的方案中造成严重问题。 例如,数据库被缩小了什么?

但是,在这样的情况下,如何做:

for(int attempts = 0; attempts < 5; attempts++)
// if you really want to keep going until it works, use   for(;;)
{
    try
    {
        DoWork();
        break;
    }
    catch { }
    Thread.Sleep(50); // Possibly a good idea to pause here, explanation below
}

<>Update: 正如尊敬的先生在以下评论中提到的: <代码>。 睡觉/编码方法中止了特定数量的微秒。 没有任何错误是完全任意的,大多数错误只能通过再打上的工作,这是因为在两部之间发生的事情发生了变化。 穿透镜的安装将为实现这一目标提供更大的机会之窗(例如,数据库发动机投入更多的时间启动)。

问题回答

类似情况

bool retry = true;
while( retry ){
  try{
    ...
    retry = false;
  }
  catch
  {
    ...
  }
  finally
  {
    ...
  }
}

只要试块的最后一条线(Retry = 虚假)运行,它就会继续。 如果出现某种例外,它将控制捕获和最后阻挡,然后回头并再次驾车。

如果你只想尝试X时间,你就可以用先试的开始值取代工业。 然后检查一下,如果在休息时间平均0次,则在休息时间开始时将其减到0次,并把它确定为试场最后线。

当然,你应该为这一空洞的捕捞区做一些事情,以便赶上你预期的例外情况,而不是赶上一切的例外情况。

Don t implement it in the catch block. Instead write a loop around it that repeats until either it was successful or some limit is reached.

类似:

bool quit = false;
int loopcount = 0;
while(!quit )
{
   try
   {
       // execute the command, might throw an exception)
       quit = true; // no exception if you got here
   }
   catch(Exception ex)
   {
      if (ex != deadlock) // doesn t work like this :-(
        quit = true;
   }
   finally
   {
      // etc.
   }
   loopcount++;
   if (loopcount > 3)
      quit = true;
}

它可能简单地把整个审判/捕获物归一旁:

while (!success) {

    try
    {
       // Call a MS SQL stored procedure (MS SQL 2000)
       // Stored Procedure may deadlock 
       success = true;
    }
    catch
    {
       // if deadlocked Call a MS SQL stored procedure (may deadlock again)
       // If deadlocked, keep trying until stored procedure executes
       success = false;
    }

}

You really shouldn t just hammer the database until it succeeds in executing your SP, but that s another story.

你可以这样做:

Boolean succeeded = false;

while (!succeeded)
{

    try
    {
        // Call a MS SQL stored procedure (MS SQL 2000)
        // Stored Procedure may deadlock 
        succeeded = true;
    }
    catch (Exception ex)
    {
        // Log
    }
}

你们可以执行Timers,检查贵家仓库程序的健康,并在同仁所说的休息室内提出答案。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签