作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用Azure,但有一个简单的try catch块
我现在正在做的是,当我们遇到任何错误时,我现在将通过电子邮件将错误消息发送给我,我想检测暂时性错误,而忽略为他们发送电子邮件
private void SomeMethod()
{
try
{
// Do stuff
}
catch (Exception ex)
{
HandleError(ex);
return RedirectToAction("Index", "Error");
}
}
protected void HandleError(Exception ex)
{
//and here i want to check if the cause of exception is not a transient error then write a code to email the error details
}
最佳答案
根据您刚刚发布的链接,并提供以下代码示例:
// Define your retry strategy: retry 3 times, 1 second apart.
var retryStrategy = new FixedInterval(3, TimeSpan.FromSeconds(1));
// Define your retry policy using the retry strategy and the Azure storage
// transient fault detection strategy.
var retryPolicy =
new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy);
// Do some work that may result in a transient fault.
try
{
// Call a method that uses Azure storage and which may
// throw a transient exception.
retryPolicy.ExecuteAction(
() =>
{
this.queue.CreateIfNotExist();
});
}
catch (Exception)
{
// All of the retries failed.
}
private void DoStuff()
{
try
{
this.DoSomethingThatCouldPotentiallyCauseTransientErrors(5);
}
catch (Exception ex)
{
// This would not be caught until the "retries" have occurred.
this.HandleException(ex);
return RedirectToAction("Index", "Error");
}
}
private void DoSomethingThatCouldPotentiallyCauseTransientErrors(int retryAttemptsBeforeExceptionThrown)
{
// Note that this will *always throw an exception*,
// I'm (attempting to) simply demonstrate my point of how the transient errors could be defined.
for (int i = 0; i < retryAttemptsBeforeExceptionThrown)
{
try
{
int x = 0;
int y = 0;
int result = x / y;
}
catch (Exception)
{
if (i < retryAttemptsBeforeExceptionThrown-1)
{
// Swallow/ignore the exception, and retry
// Note that anything hitting this block would be considered a "transient error",
// as we are not raising an exception
}
else
{
// Too many failed attempts have occurred, ***now*** we raise an exception to the caller
throw;
}
}
}
}
private void HandleException(Exception ex)
{
// Implementation
}
关于c# - 在C#异常处理中,有没有一种方法可以检测瞬时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28460113/
我是一名优秀的程序员,十分优秀!