在函数内部有一段代码try
{
    string str;
    if(str == null)
    {
          throw new System.Exception("文本为NULL");
    }
}
catch(Exception e)
{
    ....
}
在catch(Exception e) 里面就catch不到throw的这个异常了. 请勿是怎么回事呀?

解决方案 »

  1.   


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

     
      

  2.   

    to yingfeng_cyh 何解 ?
      

  3.   


     try
            {
                string str= null;
                if(str == null)
                {
                      throw new System.Exception("文本为NULL");
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
    試下這斷代碼
      

  4.   

    一般在catch中抓错误异常,楼主倒好,在try中故意抛出错误,没有你这么写的
      

  5.   

    to songxiaozhao 
    我也觉得很怪,呵呵
    只是想试试语法而已
      

  6.   

    to nj_1st_excellence  throw   new   System.Exception("文本为NULL"); 
    之后,
    外面的那个try 好像根本就catch不到 new的这个异常
      

  7.   

    查看一下 调试--异常--Common Language Runtime Exceptions 引发/用户未处理的 是否未选择?
      

  8.   

    应该是可以的,不过楼主那个可以编译通过?
    你首先先给  str 一个初始值撒,
    这里可以这样  str = null
    例子如下
    try
            {
                string str = null;
                if (str == null)
                {
                    throw new System.Exception("文本为NULL");
                }
            }
            catch (Exception ee)
            {
                Response.Write("xxx");
            }
      

  9.   

    try
    {
        string str;
        if(str == null)
        {
              throw new System.Exception("文本为NULL");
        }
    }
    catch(Exception e)
    {
        Response.Write(e.Message);
    }可以抓到你要抛的异常
      

  10.   

    myyihua 果然细心,我写帖子的时候就怕有人会有这个疑问,但还是图方便就没赋初值了....呵呵
      

  11.   

    to harryheart 
    你试试?
    我这里确实不行
      

  12.   

    你应该看一下String的==重载操作符的MSDN说明,好像这样永远为false
      

  13.   

    楼主,可以的,我试过了,你可以使用我上面那个代码,测试可以捕捉到的,
    代码再贴下
    try 
                    { 
                            string   str   =   null; 
                            if   (str   ==   null) 
                            { 
                                    throw   new   System.Exception("文本为NULL"); 
                            } 
                    } 
                    catch   (Exception   ee) 
                    { 
                            Response.Write("xxx"); 
                    }