错误陷阱问题!继承,高手请进! 是不是可以先定义自己所需的异常,再在try块里throw自己的异常,catch捉它。没有定义得就会用其父类中的异常。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 .net 中如果有错误发生,他会自动的向上抛出异常。举例来说:页面A调用B类中的方法,B类继承C类,当A调用B的方法时,在C中发生异常,你只要在A中的相应位置写Try...Catch 就能捕捉。若不写则会提示在C类中发生异常。 应该是和C++中一样,逐层向上递归。如果有类A B : AC : B在C中抛出异常,如果在C中没有try catch块,则向上寻找B中的try catch块,没找到的话则继续向上在A中寻找。 我用try{}catch{}拉,还是在父类抛出异常,大家帮我分析一下可以吗 用try catch throw应该可以捕获的呀 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(); } } 你在父类中也加了try...Cath 那当然是在父类中捕捉了阿。:)你只要把父类中的Try...Catch去掉,就会在页面层捕捉了。 你在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(); } 按你的做法根本不需要在父类中加Try..catch 去掉就好了。 Microsoft Agent 问题 如何在母版页中控制刷新时滚动条的位置不动 一个有意思的问题。。 滚动图片的问题 Asp.net GridView 中删除数据失败? 求大家帮我看看一个分页的奇怪问题?多谢了! droplist如何绑定图片 高手们,有没有p2p视频点播系统源程序 新手;中文显示的问题。 如何引用datagrid中模板列页脚中的控件呢? 微软专家在线聊天--怎样在.NET Framework中使用DataGrid Control--纪录 在datagrid中如何触发模板列的事件
若不写则会提示在C类中发生异常。
A
B : A
C : B在C中抛出异常,如果在C中没有try catch块,则向上寻找B中的try catch块,没找到的话则继续向上在A中寻找。
catch{}
拉,还是在父类抛出异常,大家帮我分析一下可以吗
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();
} }
在这个方法里没有捉的异常会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();
}