我正在从xml文件中读取信息,在某个时刻,两条Boolean.Parse()
指令可能会引发异常
考虑到整个代码段已经在try语句下,我想把所有的catch放在一个地方,而不是把上面的两个方法调用放在其他的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,然后使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将告诉异常来自哪一行。
然后修改外部catch以显示异常消息。
以下是Steve的建议,因为他告诉我更新我的答案:)
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怎么样。
TryParse方法与Parse方法类似,只是如果转换失败,TryPhase方法不会抛出异常。
因此,您可以简单地使用返回的布尔值检查故障
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
}