我想知道哪个是最好的异常处理方式,因为在我的Try
声明中,我有很多验证,如果我在那里得到一些Exception
,我的Catch
声明可以告诉我会发生什么,但我怎么知道在哪个字段发生了Exception
?异常处理最佳实践
示例代码
try
{
// If I get a Exception when converting to number,
// I will understand the error
// but how could I know where in my `Try` statement was the error ?
int valor = Convert.ToInt32(xmlnode[i].ChildNodes.Item(2).InnerText.Trim());
// A Lot of another validations here
}
Catch(Exception e)
{
this.LogInformation(e.Message);
}
对所有'Convert'抛出异常 –
,如果您不是100%确定它们是有效的,则应该使用'TryParse'。例如。对于Int32:http://msdn.microsoft.com/en-us/library/f02979c7.aspx – Bolu