为什么我的try catch不能捕捉错误??

flashasp 2005-07-07 04:34:03
现在我将添加更新记录的方法统一写在一个类里面,方便以后调用,但为什么try能生效,catch就没有反应,为何,try catch到底要怎么用,谢谢!

public int Execute(string SqlCommand)
{
int count = 0;

bool open = this.IsOpen;
if (!open)
this.Open();

SqlCommand cmd = new SqlCommand(SqlCommand, this.conn);
count = cmd.ExecuteNonQuery();

try
{
count = cmd.ExecuteNonQuery();
System.Web.HttpContext.Current.Response.Write("<script>alert('操作成功!');</script>");
}

catch (System.FormatException E)
{
System.Web.HttpContext.Current.Response.Write(E.Message);
System.Web.HttpContext.Current.Response.Write(SqlCommand);
System.Web.HttpContext.Current.Response.Write("<script langeuage=javascript>alert('SQL语句有问题啊老大!');</script>");
}


if (!open)
this.Close();

return count;
}
...全文
150 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kgdiwss 2005-07-07
  • 打赏
  • 举报
回复
try
{
}
catch(Exception Err)
{
return Err.Message.ToString();
}
flashasp 2005-07-07
  • 打赏
  • 举报
回复
^_^,粗心的家伙

try
{
SqlCommand cmd = new SqlCommand(SqlCommand, this.conn);
count = cmd.ExecuteNonQuery();
System.Web.HttpContext.Current.Response.Write("<script>alert('操作成功!');</script>");
}
catch (SqlException E)
{

System.Web.HttpContext.Current.Response.Write(E.Message);

System.Web.HttpContext.Current.Response.Write(SqlCommand);

System.Web.HttpContext.Current.Response.Write("<script langeuage=javascript>alert('SQL语句有问题老大!');</script>");
}
flashasp 2005-07-07
  • 打赏
  • 举报
回复

改成这样还是不行,try catch到底如何用呢
catch (Exception E)
or
catch (SqlException E)
gxboy 2005-07-07
  • 打赏
  • 举报
回复
其实用exception就行了。更多的时候我们只关心出错了如错向用户显示的问题,而一般不会去考虑出错的类型是什么。或者直接catch 根本不用去写异常类型。
Stvan 2005-07-07
  • 打赏
  • 举报
回复
你catch 的是System.FormatException
应该catch Exception或SqlException就可以了

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧