C#异常在同一个try / catch块中处理两次 [英] C# Exception being handled twice in same try/catch block

查看:499
本文介绍了C#异常在同一个try / catch块中处理两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我有一个异常问题,正在处理两次。

I have an exception issue which is being handled twice.

在try块中,它抛出一个HttpAntiForgeryException它被第一次捕获" HttpAntiForgeryException "捕获,它被重新抛出。

Within the try block, it is throwing a HttpAntiForgeryException, and it is being caught by the first catch "HttpAntiForgeryException", where it is being re-thrown.

然而,它正被捕获第二次捕获"例外"作为"未处理的异常",以及它再次被抛出的地方。

However, it is being caught by the second catch "Exception" as an "unhandled exception", and where it is being thrown again.

try
{
// Code throwing HttpAntiForgeryException
}
catch (HttpAntiForgeryException ex)
{
	throw;
}
catch (Exception ex)
{
	throw new CustomUnexpectedException(message: "BaseController.OnAuthorization", innerException: ex);
}

如何避免这种情况?

谢谢

杰夫在西雅图

谢谢
杰夫在西雅图

Thanks
Jeff in Seattle

推荐答案

也许是这样。

try
{
// Code throwing HttpAntiForgeryException
}
catch (Exception ex)
{
   if(ex is HttpAntiForgeryException)
      throw;
   else
      throw new CustomUnexpectedException(message: "BaseController.OnAuthorization", innerException: ex);
}


这篇关于C#异常在同一个try / catch块中处理两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆