日期:2014-05-19  浏览次数:20931 次

DDN 关于 C# throw ex; 与 throw; 的问题
~每天一个历史遗留问题~

熟悉异常的   C++++   coder,都知道
对于   throw   ex;   与   throw;   是有区别的,主要是   CLR   确定异常抛出的起点是有区别的,
如果你不清楚请参考:
《.NET   框架程序设计》Jeffery   Richter   李建忠译   chater   18.12   异常堆栈踪迹   P442   的说明

或者见
throw;与throw   ex;之间的区别  
http://blog.csdn.net/Joy_Zhao/archive/2006/10/27/1352777.aspx


但是,下面的   Button1_Click   即使使用   throw;   也堆栈信息只能跟踪到   Line   14   ,始终无法跟踪到   Line   11,而   Button2_Click   直接到   Line   21

//   .aspx.cs
protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                try   {
                        object   o   =   null;
                        int   i   =   (int)o;     //   Line   11     //   Error,   System.NullReferenceException              
                }
                catch   {
                        throw;   //   Line   14
                }              
        }

        protected   void   Button2_Click(object   sender,   EventArgs   e)
        {
                object   o   =   null;
                int   i   =   (int)o;     //   Line   21     //   Error,   System.NullReferenceException    
        }

//   .aspx
  <asp:Button   ID= "Button1 "   runat= "server "   OnClick= "Button1_Click "   Text= "Throw1 "   />    
                <asp:Button   ID= "Button2 "   runat= "server "   OnClick= "Button2_Click "   Text= "Throw2 "   />    


我做过很多测试了,对于用   try-catch-throw   捕获   CLR   运行时内部抛出的异常,即出现这种情况,始终无法跟踪到最原始的内部异常起点


何解   ?也许是我遗漏了某个知识点~

谢谢!



------解决方案--------------------
sf
------解决方案--------------------
winform测试.
单步可以调试到
int i = (int)o; // Line 11 // Error, System.NullReferenceException

------解决方案--------------------
jf
------解决方案--------------------
关注,此点同样疑问
------解决方案--------------------
没有太多的研究,一直认为只要有throw的语名的行,就是有出错信息的行了。
------解决方案--------------------
//这样就能跟踪到你想要的位置(Line11==Line4);
protected void throwErr()
{
object o = null;
int i = (int)o; //Line4 //Error, System.NullReferenceException