惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 大尾巴狼

列相等与不等 (转载)一个项目经理的一些个人体会 数据库对象搜索器 XML遭遇缺省Namespace - 大尾巴狼 - 博客园 打开Outlook发邮件界面,发邮件 - 大尾巴狼 - 博客园 下载DataGrid内容,作为Excel可打开的文件 迁移用户的Documents and Settings 利用自定义属性,定义枚举值的详细文本,完美解决 - 大尾巴狼 - 博客园 利用自定义属性,定义枚举值的详细文本 多态相关的关键字的总结 住房商业贷款计算器 关于设置IIS目录的属性问题 只返回DB架构信息 用script让IE窗口最大化 - 大尾巴狼 - 博客园 可在多线程下TextBox中显示信息,和控制滚动的一个函数 DNN文件夹说明 脚本生成Guid - 大尾巴狼 - 博客园 DNN页面呈现过程 获得一个指定星期的起始和终止日期 - 大尾巴狼 - 博客园
为什么有时不能正确定位到异常的发生位置?
大尾巴狼 · 2007-06-30 · via 博客园 - 大尾巴狼

在我们定位异常发生位置的时候,一般都能得到正确的异常信息(Exception.Message)。
但是当根据调用堆栈来定位异常发生点的时候,却往往得不到正确的位置,即正确的调用堆栈。
这与再次抛出异常的方式有关,请看下面的程序和输出结果。

在foo_ABC中捕获、处理、并重新抛出异常。在Main函数中最终来处理异常,获得详细的异常信息。
当使用 throw ex 后,在异常的调用堆栈里就不是正确正确的异常发生位置。
如果有源代码则可以顺着继续找,如果没有的话,则不好正确定位异常的发起的根源。

结论:
异常的调用堆栈,是以发生异常或者throw ex为起点的,当使用throw ex时会覆盖掉发生异常时产生的调用堆栈。
所以, foo_A()函数里抛出异常的方式要慎用!

  1 using System;
  2 
  3 namespace ConsoleApplication18
  4 {
  5     /// <summary>
  6     /// Summary description for Class1.
  7     /// </summary>
  8     class Class1
  9     {
 10         /// <summary>
 11         /// The main entry point for the application.
 12         /// </summary>
 13         [STAThread]
 14         static void Main(string[] args)
 15         {
 16             try
 17             {
 18                 foo_A();
 19             }
 20             catch(Exception ex)
 21             {
 22                 Console.WriteLine("=================Main");
 23                 Console.WriteLine(ex.ToString());
 24                 Console.WriteLine("");Console.WriteLine("");
 25             }
 26 
 27             try
 28             {
 29                 foo_B();
 30             }
 31             catch(Exception ex)
 32             {
 33                 Console.WriteLine("=================Main");
 34                 Console.WriteLine(ex.ToString());
 35                 Console.WriteLine("");Console.WriteLine("");
 36             }
 37 
 38             try
 39             {
 40                 foo_C();
 41             }
 42             catch(Exception ex)
 43             {
 44                 Console.WriteLine("=================Main");
 45                 Console.WriteLine(ex.ToString());
 46                 Console.WriteLine("");Console.WriteLine("");
 47             }
 48 
 49             int i = Console.Read();
 50         }
 51 
 52         private static void foo()
 53         {
 54             throw new Exception("happend in foo");
 55         }
 56 
 57         private static void foo1()
 58         {
 59             foo();
 60         }
 61 
 62         private static void foo_A()
 63         {
 64             try
 65             {
 66                 foo1();
 67             }
 68             catch(Exception ex)
 69             {
 70                 Console.WriteLine("=================foo_A   throw ex;");
 71                 Console.WriteLine(ex.ToString());
 72                 
 73                 throw ex;
 74 
 75             }
 76         }
 77 
 78         private static void foo_B()
 79         {
 80             try
 81             {
 82                 foo1();
 83             }
 84             catch(Exception ex)
 85             {
 86                 Console.WriteLine("=================foo_B  throw");
 87                 Console.WriteLine(ex.ToString());
 88                 
 89                 throw;
 90             }
 91         }
 92 
 93         private static void foo_C()
 94         {
 95             try
 96             {
 97                 foo1();
 98             }
 99             catch(Exception ex)
100             {
101                 Console.WriteLine("=================foo_C  throw new Exception(\"throw again by new\",ex)");
102                 Console.WriteLine(ex.ToString());
103                 
104                 throw new Exception("throw again by new",ex);
105             }
106         }
107     }
108 }

输出结果:

=================foo_A   throw ex;

System.Exception: happend in foo

   at ConsoleApplication18.Class1.foo() in e:\my documents\visual studio project

s\consoleapplication18\class1.cs:line 54

   at ConsoleApplication18.Class1.foo1() in e:\my documents\visual studio projec

ts\consoleapplication18\class1.cs:line 59

   at ConsoleApplication18.Class1.foo_A() in e:\my documents\visual studio proje

cts\consoleapplication18\class1.cs:line 66

=================Main

System.Exception: happend in foo

   at ConsoleApplication18.Class1.foo_A() in e:\my documents\visual studio proje

cts\consoleapplication18\class1.cs:line 73

   at ConsoleApplication18.Class1.Main(String[] args) in e:\my documents\visual

studio projects\consoleapplication18\class1.cs:line 18

=================foo_B  throw

System.Exception: happend in foo

   at ConsoleApplication18.Class1.foo() in e:\my documents\visual studio project

s\consoleapplication18\class1.cs:line 54

   at ConsoleApplication18.Class1.foo1() in e:\my documents\visual studio projec

ts\consoleapplication18\class1.cs:line 59

   at ConsoleApplication18.Class1.foo_B() in e:\my documents\visual studio proje

cts\consoleapplication18\class1.cs:line 82

=================Main

System.Exception: happend in foo

   at ConsoleApplication18.Class1.foo() in e:\my documents\visual studio project

s\consoleapplication18\class1.cs:line 54

   at ConsoleApplication18.Class1.foo1() in e:\my documents\visual studio projec

ts\consoleapplication18\class1.cs:line 59

   at ConsoleApplication18.Class1.foo_B() in e:\my documents\visual studio proje

cts\consoleapplication18\class1.cs:line 89

   at ConsoleApplication18.Class1.Main(String[] args) in e:\my documents\visual

studio projects\consoleapplication18\class1.cs:line 29

=================foo_C  throw new Exception("throw again by new",ex)

System.Exception: happend in foo

   at ConsoleApplication18.Class1.foo() in e:\my documents\visual studio project

s\consoleapplication18\class1.cs:line 54

   at ConsoleApplication18.Class1.foo1() in e:\my documents\visual studio projec

ts\consoleapplication18\class1.cs:line 59

   at ConsoleApplication18.Class1.foo_C() in e:\my documents\visual studio proje

cts\consoleapplication18\class1.cs:line 97

=================Main

System.Exception: throw again by new ---> System.Exception: happend in foo

   at ConsoleApplication18.Class1.foo() in e:\my documents\visual studio project

s\consoleapplication18\class1.cs:line 54

   at ConsoleApplication18.Class1.foo1() in e:\my documents\visual studio projec

ts\consoleapplication18\class1.cs:line 59

   at ConsoleApplication18.Class1.foo_C() in e:\my documents\visual studio proje

cts\consoleapplication18\class1.cs:line 97

   --- End of inner exception stack trace ---

   at ConsoleApplication18.Class1.foo_C() in e:\my documents\visual studio proje

cts\consoleapplication18\class1.cs:line 104

   at ConsoleApplication18.Class1.Main(String[] args) in e:\my documents\visual

studio projects\consoleapplication18\class1.cs:line 40