有没有使用嵌套异常的方法?我有这样的代码“第一层”:
my func()
{
try
{
resp = Web.FormPostRequest(auth_url, authPost, "", url, cookies, type, proxy_string);
}
catch (Exception ex)
{
if (ex is xNet.ProxyException)
{
throw new xNet.ProxyException();
}
if(ex is xNet.NetException)
{
//MessageBox.Show(ex.Message);
throw new xNet.NetException();
//return false;
}
}
}在“更高层次”,我得到了这个:
try
{
result = myfunc(auth_data[0], auth_data[1], Form1.domain, type, proxy_string);
break;
}
catch (xNet.NetException ex)
{
{
result = false;
MessageBox.Show(ex.Message);
return;
}
}协议是调试器总是说excpetion xNet.NetException没有上线
if(ex is xNet.NetException)
{
//MessageBox.Show(ex.Message);
throw new xNet.NetException();
//return false;
}单位: myfunc()它会是什么,我如何解决这个问题?谢谢!
我将代码更改为最简单:
try
{
resp = Web.FormPostRequest(auth_url, authPost, "", url, cookies, type, proxy_string);
}
catch (Exception ex)
{
throw;
}但是即使是现在,视觉研究也说有xNet.NetException没有被处理。
发布于 2016-01-12 17:55:17
你能不能抓住你想要捕捉的具体异常,让剩下的“流过”?
catch (xNet.ProxyException ex) {
//re-throwing per the comment(s) in a smarter guy's answer.
throw new Exception("Adding really important info here.",ex);
}
catch (xNet.NetException exToo) {
//MessageBox.Show(ex.Message);
//return false;
//blah .. blah ..
}在SQL异常情况下,我们会这样做,而且效果非常好。它允许我们根据SQL server抛出的特定错误(抱歉在堆栈post中使用“抛出- off”)捕获要捕获的数据.(漫长的一天)
有一些规则可以管理多个捕获块,但是我太懒了,不能在这里打字。
此外,为了扩大这个答案以包括一个未被问到的问题,下面的格式也适用于c# 6。
catch (Exception ex) when (ex is xNet.ProxyException || ex is xNet.NetException) {
//please go away...
}不管怎样,你只抓到你想要处理的东西,对吧?然后,如果需要外部catch块中的堆栈,请查看innerException (因为我们正在为新数据创建一个小的嵌套异常)。
最后(小调)备注(编辑)
根据注释,您在原始问题中提供的示例代码似乎是抛出一种错误类型,但外部catch块捕获的是另一种错误类型。
内部函数:
throw new xNex.ProxyException();外部函数:
catch(xNet.NetException ex) ...也许这仅仅是因为你做了一些编辑来使它更容易阅读,但也许检查你的代码呢?当你被拨打电话时,很难看出这些错误!
发布于 2016-01-12 17:48:54
除非您有一个很好的理由创建新异常并吞并除这两种类型之外的所有类型,否则我只需要更改您的catch块,以便在所有情况下重新抛出原始异常:
catch (xNet.ProxyException ex)
{
// is there something you want to do here?
throw; // let the caller handle all exceptions.
}
catch(xNet.NetException ex)
{
// is there something you want to do here?
throw; // let the caller handle all exceptions.
}
catch
{
throw; // let the caller handle all exceptions.
}如果您不打算在任何一个处理程序中执行任何操作,那么您只需删除整个catch块,然后自然地让异常冒泡起来。
发布于 2016-01-12 17:50:17
我该怎么解决这个问题?
首先要做的是删除整个catch块:
catch (Exception ex)
{
if (ex is xNet.ProxyException)
{
throw new xNet.ProxyException();
}
if(ex is xNet.NetException)
{
//MessageBox.Show(ex.Message);
throw new xNet.NetException();
//return false;
}
}它不仅实际上没有做任何事情来处理异常,而且还显式地丢弃了异常,并将其替换为没有任何有关所发生错误的信息的异常。
因此,生成的方法很简单:
void MyFunc()
{
resp = Web.FormPostRequest(auth_url, authPost, "", url, cookies, type, proxy_string);
}如果方法确实有一些信息要添加到异常中,那么至少应该保留原始异常:
void MyFunc()
{
try
{
resp = Web.FormPostRequest(auth_url, authPost, "", url, cookies, type, proxy_string);
}
catch (Exception ex)
{
// do something here?
throw new SomeExceptionType(ex);
}
}或者,如果没有需要添加的新信息(但仍然有一些有意义的事情要做),至少重新抛出原始异常:
void MyFunc()
{
try
{
resp = Web.FormPostRequest(auth_url, authPost, "", url, cookies, type, proxy_string);
}
catch (Exception ex)
{
// do something here
throw;
}
}但是,除非有什么事情你可以实际处理或以任何方式对异常作出反应,否则就不要费心去抓它了。它只应该被能够有意义地处理它的代码捕获。
https://stackoverflow.com/questions/34750345
复制相似问题