C#异常信息获取

try
{
    int i = 0;
    int a = 10 / i;
}
catch (Exception ex)
{
    /**
     * 1.异常消息
     * 2.异常模块名称
     * 3.异常方法名称
     * 4.异常行号
     */
    String str = "";
    str += ex.Message + "
";//异常消息
    str += ex.StackTrace + "
";//提示出错位置,不会定位到方法内部去
    str += ex.ToString() + "
";//将方法内部和外部所有出错的位置提示出来
    MessageBox.Show(str);
} 

记录下来主要是为了记录系统异常日志所使用

原文地址:https://www.cnblogs.com/duanjt/p/5435246.html