c#中嵌套的异常

本文关键字:异常 嵌套 | 更新日期: 2023-09-27 18:29:57

是否有使用嵌套异常的方法?我有这个代码"一级":

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;
}

但即使是现在,visualstuido也表示有xNet.NetException没有得到处理。

c#中嵌套的异常

您可以只捕获您想要捕获的特定异常并让其余的异常"通过"吗?

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抛出的特定错误准确地捕获我们想要捕获的数据(很抱歉在堆栈帖子中使用了"through-off"…漫长的一天)。

有一些规则管理多个catch块,但我太懒了,没有在这里键入它们。

此外,为了将这个答案扩展到包括一个未被问到的问题,下面的格式也适用于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) ...

也许这只是因为你做了一些编辑,让它更容易阅读,但也许检查一下你的代码?当你接通电话时,很难看出那些错误!

除非你有充分的理由创建新的异常并吞下除这两种类型之外的所有异常,否则我只会更改你的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块,让异常自然出现。

我该如何解决此问题?

好吧,你应该做的第一件事是删除整个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);
    }
}

或者,如果没有新的信息要添加(但对do仍然有意义),至少重新抛出原始异常:

void MyFunc()
{
    try
    {
        resp = Web.FormPostRequest(auth_url, authPost, "", url, cookies, type, proxy_string);
    }
    catch (Exception ex)
    {
        // do something here
        throw;
    }
}

但是,除非你能做些什么来真正处理或以任何方式响应异常,否则不要麻烦捕捉它。它应该只被能够有意义地处理它的代码捕捉。