请问如何让Exception返回出错地方的行号

cs12356 2010-04-10 03:25:54
谢谢
...全文
935 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangxianshou 2010-04-10
  • 打赏
  • 举报
回复
虽然自己也经常try catch
还没有怎么注意过呢,学习了
wuyq11 2010-04-10
  • 打赏
  • 举报
回复
StackTrace st = new StackTrace(new StackFrame(true));
Console.WriteLine(st.ToString());
Console.WriteLine(st.GetFrame(0).GetFileLineNumber().ToString());
mirmuo 2010-04-10
  • 打赏
  • 举报
回复
我想知道如何让程序流程返回抛出异常的地方继续执行
深海之蓝 2010-04-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xingyuebuyu 的回复:]
C# code
try
{
int t = 1;
int i = 5 / --t;
}
catch (Exception e1)
{

System.Diagnost……
[/Quote]
这个不错
ongjfg001 2010-04-10
  • 打赏
  • 举报
回复
谢谢!!回复看看!!
xingyuebuyu 2010-04-10
  • 打赏
  • 举报
回复
            try
{
int t = 1;
int i = 5 / --t;
}
catch (Exception e1)
{

System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(e1,true);
int i= st.GetFrame(0).GetFileLineNumber();
int j = st.GetFrame(0).GetFileColumnNumber();
string k = st.GetFrame(0).GetFileName();
}
人工智能算法 2010-04-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 foxdave 的回复:]
输出e.toString 里面有
不要截取字符串 根据VS版本会有不一样的显示
[/Quote]

同意这位的意思, 如果是en版的.net 则需要截获:line的位置
Justin-Liu 2010-04-10
  • 打赏
  • 举报
回复
输出e.toString 里面有
不要截取字符串 根据VS版本会有不一样的显示
人工智能算法 2010-04-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ojlovecd 的回复:]
堆栈信息里就有啊:

C# code


try
{
throw new Exception();
}
catch (Exception e)
{

Console.WriteLine(e.S……
[/Quote]


被人抢先了

不过还是详细说一下吧:


static void Main(string[] args)
{
try
{
int t = 1;
int i = 5 / --t;
}
catch (Exception e)
{
int i = e.StackTrace.IndexOf("行号");
string s = e.StackTrace.Substring(i+3);
i = s.IndexOf(' ');
if (i != -1)
{
s = s.Substring(0, i);
}
//这里s已经存储了错误所在行号
}
}
我姓区不姓区 2010-04-10
  • 打赏
  • 举报
回复
堆栈信息里就有啊:


try
{
throw new Exception();
}
catch (Exception e)
{

Console.WriteLine(e.StackTrace);
}
xnhsdp 2010-04-10
  • 打赏
  • 举报
回复
等楼下高手

110,533

社区成员

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

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

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