为了在系统发生异常时记录自定义的信息和参数,所以写了一个自己的异常类。这个异常类需要记录整个StackTrace信息。
但Exception.StackTrace只能保存最后一次抛出异常方法的堆栈信息。因为之前抛出异常的方法已经出栈了,无法获得StackTrace。
所以重写了StackTrace方法,如下:namespace MyException
{
class Program
{
static void Main(string[] args)
{
try
{
testA();
}
catch (SelfException selfex)
{
Console.WriteLine(selfex.StackTrace);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
finally
{
Console.ReadKey();
} } static void testA()
{
try
{
// 可能发生SqlException 异常
testB();
}
catch (SqlException ex)
{
// 捕获SqlException异常,用自定义异常继续抛出
throw new SelfException(ex,"数据库异常+异常代码.....");
}
} static void testB()
{
// 程序运行出错,抛出SqlException异常
throw new SqlException();
}
} // 自定义异常类
class SelfException : Exception
{
private string _trace;
private string _message; public SelfException(Exception ex,string message)
{
_trace = ex.StackTrace;
_message = message;
} public override string StackTrace
{
get
{
return _trace + base.StackTrace;
}
}
}
}我在自己的类中设置了一个_trace来保存前面方法的StackTrace信息,然后和当前的StackTrace相加。实际效果也是对的,如下:在 MyException.Program.testB() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 45
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 35
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 39
在 MyException.Program.Main(String[] args) 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 13但是在调试时发现很奇怪的问题
调试到return _trace + base.StackTrace; 这一句时
_trace为
在 MyException.Program.testB() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 45
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 35 base.StackTrace为
在 MyException.Program.testB() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 45
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 35
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 39
在 MyException.Program.Main(String[] args) 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 13但是实际输出的又不是连接后的内容。很奇怪。按道理来说base.StackTrace应该只是
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 39
在 MyException.Program.Main(String[] args) 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 13大家帮看看
但Exception.StackTrace只能保存最后一次抛出异常方法的堆栈信息。因为之前抛出异常的方法已经出栈了,无法获得StackTrace。
所以重写了StackTrace方法,如下:namespace MyException
{
class Program
{
static void Main(string[] args)
{
try
{
testA();
}
catch (SelfException selfex)
{
Console.WriteLine(selfex.StackTrace);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
finally
{
Console.ReadKey();
} } static void testA()
{
try
{
// 可能发生SqlException 异常
testB();
}
catch (SqlException ex)
{
// 捕获SqlException异常,用自定义异常继续抛出
throw new SelfException(ex,"数据库异常+异常代码.....");
}
} static void testB()
{
// 程序运行出错,抛出SqlException异常
throw new SqlException();
}
} // 自定义异常类
class SelfException : Exception
{
private string _trace;
private string _message; public SelfException(Exception ex,string message)
{
_trace = ex.StackTrace;
_message = message;
} public override string StackTrace
{
get
{
return _trace + base.StackTrace;
}
}
}
}我在自己的类中设置了一个_trace来保存前面方法的StackTrace信息,然后和当前的StackTrace相加。实际效果也是对的,如下:在 MyException.Program.testB() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 45
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 35
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 39
在 MyException.Program.Main(String[] args) 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 13但是在调试时发现很奇怪的问题
调试到return _trace + base.StackTrace; 这一句时
_trace为
在 MyException.Program.testB() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 45
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 35 base.StackTrace为
在 MyException.Program.testB() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 45
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 35
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 39
在 MyException.Program.Main(String[] args) 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 13但是实际输出的又不是连接后的内容。很奇怪。按道理来说base.StackTrace应该只是
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 39
在 MyException.Program.Main(String[] args) 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 13大家帮看看
而是定义一个MyStackTrace public string MyStackTrace
{
get
{
return _trace + base.StackTrace;
}
}这个时候在调试,到这里,就发现
_trace为
在 MyException.Program.testB() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 45
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 35 base.StackTrace是
在 MyException.Program.testA() 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 39
在 MyException.Program.Main(String[] args) 位置 E:\program\CSharp\Practice\MyException\MyException\Program.cs:行号 13 不明白为什么用override重写时的base.StackTrace和现在的不一样,但输出的结果又一样,大家可以看下