在一个web页的pageload里面,   try
   {   }
   catth
   {   }   如果出现错误,我怎么把执行前面的资源释放?并组中止这个页,结束输出 谢谢

解决方案 »

  1.   

    try
    {
      ;
    }
    catch
    {
      ;
    }
    finally
    {
      //在此输入要做的工作
    }
      

  2.   

    Catch(System.IO.Exception e)
    {}
    Catch(System.Db.SqlExcetion e)
    {}
    这样把你要处理的错误分类处理
      

  3.   

    再问一个问题:  //try
            //{
                newpassport.Service mycom = new newpassport.Service();            string temp = mycom.SetGold(Session["username"].ToString(), Session["password"].ToString(), 1, 2, "应聘职位");            if (temp == "GOLD")
                {
                    Sp mysp = new Sp();
                    //try
                    //{
                        mysp.Execsql("insert into passport_teacherjob (jstudent,jcompany,jcompanyid) values('" + Session["username"].ToString() + "','" + jobinf[1].ToString() + "','" + jobinf[0].ToString() + "')");
                       // Response.Redirect(Session["url"].ToString());
                        Response.Redirect("joblist.aspx?page=1");
                    //}在以上的代码中,如果我加入try 进行异常处理 ,会跳转到error页,而如果没有加入try处理,代码也是执行正常,记录数据并进入列表员,并没有出现错误,我想问一下,怎么会这样?用try会有问题,没有用错不会,郁闷
                    //catch
                    //{                //    mysp.conn.Close();
                    //  Response.Write("333");
                    // //   Response.Redirect("error.aspx");                //}
                }            else
                {
                    if (temp == "NOT GOLD")
                    {
                        Label1.Visible = true;
                        Label1.Text = "点数不足";
                    }
                    if (temp == "ERROR PASSWORD")
                    {                    Label1.Visible = true;
                        Label1.Text = "帐号与密码不符,请退出后重新登陆";
                    }            }
            //}
            //catch
            //{
            //    //   Response.Write("333");
            //    Response.Redirect("error.aspx");
            //}
      

  4.   

    一般的Try和Catch错误都由原因的,你在Catch后添加Exception对象,单步调试一下抛出的是什么错误,然后在决定是否有必要添加Try...catch
    形式如下:
    try
    {
       mysp.Execsql("insert into passport_teacherjob (jstudent,jcompany,jcompanyid) values('" + Session["username"].ToString() + "','" + jobinf[1].ToString() + "','" + jobinf[0].ToString() + "')"); 
    }
    catch(Exception ex)

       System.Diagnostics.Debug.Assert(false, ex.Message, ex.StackTrace); 
    //    mysp.conn.Close(); 
    //  Response.Write("333"); 
    //   Response.Redirect("error.aspx"); 
    }
    看看它抛出什么异常,然后改正之后,就可以不需要try...catch了。