请教:有关Exception类

hsghxm 2006-12-25 10:53:01
假设在一个程序的所有类的各方法属性等等中都没有加异常处理,现在想知道如何能获取引起该程序报错时的类名以及该类的方法名(不需要知道是什么错误类型)?

目前我知道用以下方法可以得到我想得到的
try{}catch(Exception ){Console.WriteLine(ex.Source);}可是工作量太大了,100多个类,每个类有那么多方法,不想一个一个的去做,现在请教大家有没有好的办法?
...全文
138 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2006-12-25
  • 打赏
  • 举报
回复
catch (Exception)
{
// Create a StackTrace that captures
// filename, line number and column
// information, for the current thread.
StackTrace st = new StackTrace(true);
for(int i =0; i< st.FrameCount; i++ )
{
// High up the call stack, there is only one stack frame
StackFrame sf = st.GetFrame(i);
Console.WriteLine("\nHigh Up the Call Stack, Method: {0}",
sf.GetMethod() );

Console.WriteLine( "High Up the Call Stack, Line Number: {0}",
sf.GetFileLineNumber());
}
}
hsghxm 2006-12-25
  • 打赏
  • 举报
回复
请问,上面的代码放在哪里使用?
我并不想每个方法都添加异常处理的,那样做很麻烦的!
最好能在主类中使用一次,就可以把其他类都能进行异常处理!

谢谢楼上的


110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧