是否有可能在C#asp.net中捕获所有异常? [英] is it posssible to catch all the exception in C# asp.net?

查看:237
本文介绍了是否有可能在C#asp.net中捕获所有异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net,C#,VS 2005,SQL Server 2005..

是否可以在C#asp.net中捕获所有异常?

我正在使用

i m using asp.net,C#,VS 2005,sql server 2005..

is it posssible to catch all the exception in C# asp.net?

i m using

try
{
}
catch(Exception ex)
{
}



但是此try块无法捕获InvalidOperationException等...

有没有办法使用一个catch块来捕获所有异常?

请帮助我

问候
卡兰

摘自OP的评论:



but this try block does not catch InvalidOperationException etc...

is there any way to catch all exception using one catch block?

plz help me

regards
karan

From a comment from the OP:

try 
{
  connection.Open();
  st = connection.BeginTransaction();
  commandToFinding = new SqlCommand(sqlqueryToFinding, connection,st);
  if (numToFinding != 0)
  {
    st.Commit();
    Response.Redirect("AddStudy.aspx?stid=" + StudyIDTextBox.Text);
    connection.Close();
  }
}
catch(Exception)
{
  st.Rollback(); // Exception happens here (This SqlTransaction has completed; it is no longer usable.)
  connection.Close();
}

推荐答案

我不同意您的意见. InvalidOperationException继承自Exception类.抛出的任何异常都将由您显示的代码块来处理.
I do not agree with you. InvalidOperationException is inherited from Exception class. Any excpetion thrown would be handled by the code block you have shown.


也许您正在捕获一个异常,然后在catch块中引发了另一个异常?毕竟,根据对其他答案的评论,您正在catch块中执行数据库操作(回滚并关闭连接).我没看过这些操作是否会引发您的特定异常,但是您应该检查一下.
Perhaps you''re catching an exception and then throwing another exception in your catch block? After all, according to your comments to other answers, you''re performing DB operations in your catch block (a rollback and closing a connection). I haven''t looked to see if either of those operations could throw your particular exception, but you should check.


您是否考虑过使用多个catch块?
您为什么不按以下步骤抓挡并尝试?
Did you consider Making use of Multiple catch blocks ??

Why dont you make you catch block as follows and try ?
catch ( InvalidOperationException e )
{
 // Perform task
}
catch ( Exception e )
{
 // Perform Task
}
finally
{
// Perform Talk
}


BR//
Harsha


BR//
Harsha


这篇关于是否有可能在C#asp.net中捕获所有异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆