知道哪个方法抛出异常

Knowing which method is throwing the exception

我正在从一个 xml 文件中读取信息,有时两个 Boolean.Parse() 指令可能会抛出异常

鉴于整个片段已经在 try 语句下,我想将所有捕获放在一个地方,而不是将上面的两个方法调用放在其他 try-catch 下。

我的问题是如何知道哪个方法抛出异常?我必须根据转换失败的用户显示不同的错误消息。

try
{
    XmlNode db = _xml.SelectSingleNode("root/database");


    string usr = db.SelectSingleNode("username").InnerText;
    string psw = db.SelectSingleNode("password").InnerText;
    string srvr = db.SelectSingleNode("server").InnerText;
    string dbn = db.SelectSingleNode("dbname").InnerText;

    //Here a FormatException can be thrown by both these Parse()
    //And I need to know which is the caller in order to display the correct error message
    bool clean_db = Boolean.Parse(db.Attributes["clean"].Value);
    bool functions = Boolean.Parse(db.Attributes["insertFunctions"].Value);


    return new DatabaseConfiguration(usr, psw, srvr, dbn, clean_db, functions);
}
catch (XPathException)
{
    Console.WriteLine("<database> node is missing");
}
catch(FormatException e)
{
    //Here I'm supposed to do something to get the caller
    Console.WriteLine("Error message");
}

在每个 boolean.parse 方法周围添加一个额外的 try/catch,然后捕获:

try
{
    XmlNode db = _xml.SelectSingleNode("root/database");


    string usr = db.SelectSingleNode("username").InnerText;
    string psw = db.SelectSingleNode("password").InnerText;
    string srvr = db.SelectSingleNode("server").InnerText;
    string dbn = db.SelectSingleNode("dbname").InnerText;

    //Here a FormatException can be thrown by both these Parse()
    //And I need to know which is the caller in order to display the correct error message
    bool clean_db;

        try
        {
            clean_db = Boolean.Parse(db.Attributes["clean"].Value);
        }
        catch
        {
            throw new Exception ("clean exception");
        }

        bool functions;

        try
        {
            functions = Boolean.Parse(db.Attributes["insertFunctions"].Value);
        }
        catch
        {
            throw new Exception ("function exception");
        }

        return new DatabaseConfiguration(usr, psw, srvr, dbn, clean_db, functions);
    }
    catch (XPathException)
    {
        Console.WriteLine("<database> node is missing");
    }
    catch(FormatException e)
    {
        //Here I'm supposed to do something to get the caller
        Console.WriteLine("Error message");
    }

然后外层的catch会告诉异常来自哪一行。

然后修改您的外部捕获以显示异常消息。

这是史蒂夫的建议,因为他让我更新我的答案:)

try
{
    XmlNode db = _xml.SelectSingleNode("root/database");


    string usr = db.SelectSingleNode("username").InnerText;
    string psw = db.SelectSingleNode("password").InnerText;
    string srvr = db.SelectSingleNode("server").InnerText;
    string dbn = db.SelectSingleNode("dbname").InnerText;

    //Here a FormatException can be thrown by both these Parse()
    //And I need to know which is the caller in order to display the correct error message
    bool clean_db = ParseDbAttributeValue(db.Attributes["clean"].Value);

    bool functions = ParseDbAttributeValue(db.Attributes["insertFunctions"].Value);

    return new DatabaseConfiguration(usr, psw, srvr, dbn, clean_db, functions);
}
catch (XPathException)
{
    Console.WriteLine("<database> node is missing");
}
catch(FormatException e)
{
    //Here I'm supposed to do something to get the caller
    Console.WriteLine("Error message");
}

private bool ParseDbAttributeValue(object myValue)
{
    return Boolean.Parse(myValue);
}

使用 TryParse.

怎么样

The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.

因此您可以简单地使用返回的布尔值检查是否失败

 bool clean_db;
 if(!Boolean.TryParse(db.Attributes["clean"].Value),out clean_db)
 {
    // Failled 
 }

 bool functions;
 if(!Boolean.TryParse(Boolean.Parse(db.Attributes["insertFunctions"].Value,out functions)
 {
   // Failled
 }