我正在从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");
}
答案 0 :(得分:2)
在每个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以显示异常消息。
以下是史蒂夫的建议,因为他告诉我更新我的答案:)
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);
}
答案 1 :(得分:1)
如何使用TryParse。
TryParse方法类似于Parse方法,但如果转换失败,TryParse方法不会抛出异常。
因此,您只需使用返回的布尔值
检查失败 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
}