是不是可以先定义自己所需的异常,再在try块里throw自己的异常,catch捉它。没有定义得就会用其父类中的异常。

解决方案 »

  1.   

    .net 中如果有错误发生,他会自动的向上抛出异常。举例来说:页面A调用B类中的方法,B类继承C类,当A调用B的方法时,在C中发生异常,你只要在A中的相应位置写Try...Catch 就能捕捉。
    若不写则会提示在C类中发生异常。
      

  2.   

    应该是和C++中一样,逐层向上递归。如果有类

    B : A
    C : B在C中抛出异常,如果在C中没有try catch块,则向上寻找B中的try catch块,没找到的话则继续向上在A中寻找。
      

  3.   

    我用try{}
    catch{}
    拉,还是在父类抛出异常,大家帮我分析一下可以吗
      

  4.   

    用try catch throw应该可以捕获的呀
      

  5.   

    TrainPlan trainplan =new TrainPlan();
    try
    {
    trainplan.DeleteTrainPlan(strid);
    }
    catch(System.Data.SqlClient.SqlException erro)
    {
    Response.Write("<script>alert('培训计划已经启动,不能删除')</script>");
    }//trainplan的一个方法
    public void DeleteTrainPlan(string strid)
    {
    //删除计划
    int intRows;
    string strsql="delete from trainplan where trainplan_id='"+ strid +"'";
    intRows=ExecuteSql(strsql);
         
    }
    //父类
    protected static int ExecuteSql(string strSql)
    {
    //执行正增加,删除,修改操作
    SqlConnection conn=new SqlConnection(strConn);
    SqlCommand cmd=new SqlCommand(strSql,conn);
    try
    {
    conn.Open();
    int row=cmd.ExecuteNonQuery();
    return row;
    }
    catch(System.Data.SqlClient.SqlException er)
    {
    throw new Exception(er.Message);
    }
    finally
    {
    cmd.Dispose();
    conn.Close();

    } }
      

  6.   

    你在父类中也加了try...Cath 那当然是在父类中捕捉了阿。:)你只要把父类中的Try...Catch去掉,就会在页面层捕捉了。
      

  7.   

    你在int ExecuteSql(string strSql)这里捉到一个异常,又在异常里throw一个普通的异常。最后是父类的普通常,
    在这个方法里没有捉的异常会throw一个普通异常!
    可以这样子:
    try
    {
    conn.Open();
    int row=cmd.ExecuteNonQuery();
    return row;
    }
    catch(System.Data.SqlClient.SqlException er)
    {
    throw new Exception(er.Message);
    }
    catch(Exception ex)
    {
     return ;//捉普通的异常,不作任何处理,返回,当然也可以doign something.或是多个catch.
    }
    finally
    {
    cmd.Dispose();
    conn.Close();

    }
      

  8.   

    按你的做法根本不需要在父类中加Try..catch 去掉就好了。