何时使用、何时不使用 Try Catch Final
我正在 .net 3.5 中创建 asp.net Web 应用程序,我想知道何时使用以及何时不使用 Try Catch Final 块?特别是,我的大部分 try catch 都围绕着执行存储过程和填充文本字段或网格视图?当您执行存储过程并填充数据显示控件时,您会每次使用Try Catch吗?
我的代码块通常如下所示:
protected void AddNewRecord()
{
try
{
//execute stored proc
// populate grid view controls or textboxes
}
catch (Exception ex)
{
//display a messagebox to user that an error has occured
//return
}
finally
{ }
}
I am creating asp.net web apps in .net 3.5 and I wanted to know when to use and when not to use Try Catch Finally blocks? In particular, a majority of my try catch's are wrapped around executing stored procs and populating textfields or gridviews? Would you use Try Catch EVERYTIME when you execute a stored proc and populated a data display control?
My code block usually looks like:
protected void AddNewRecord()
{
try
{
//execute stored proc
// populate grid view controls or textboxes
}
catch (Exception ex)
{
//display a messagebox to user that an error has occured
//return
}
finally
{ }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
答案是“这取决于”。
您可能希望在每个原子操作周围使用
try{...} catch {...}
,以便在出现问题时可以回滚到上一个良好状态(使用事务)。这可能是一个或多个存储过程 - 这取决于您的应用程序。如果要捕获异常,请确保明确捕获了哪些异常。您不应该使用
catch (Exception ex)
或catch()
- 称为“捕获所有”异常处理 - 但应使用特定的 catch 语句,例如catch (IndexOutOfRangeException ex)
(例如)。但是,如果您无法处理异常或者无法清理异常,那么您不应该捕获它。
The answer is "it depends".
You might want to use a
try{...} catch {...}
around every atomic operation so that if there is a problem you can roll back to the last good state (using transactions). This may be one or several stored procedures - it depends on your application.If you are trapping the exception, make sure you are explicit in which exceptions you catch. You shouldn't have
catch (Exception ex)
orcatch()
- known as "catch all" exception handling - but have specific catch statements likecatch (IndexOutOfRangeException ex)
(for example) instead.However, if you can't handle the exception or there's nothing you can do to clean up, then you shouldn't trap it.
当您打算在 catch 块中处理异常时,应该只使用
try catch
。我所说的句柄的意思是,记录错误,由于错误而选择不同的路径等。如果您只是打算重新抛出它,则尝试捕获是没有意义的。You should only use
try catch
, when you intend on handling the exception in the catch block. What I mean by handle is, log the error, choose a different path because of the error etc. If you merely intend on re-throwing it, there is no point in having try catch.正如其他人所说,这取决于情况。我倾向于在两种情况下使用 try/catch/finally 块:
我需要以某种方式处理异常,而不是简单地重新抛出它。
我需要清理
finally
块中的一些资源。除了这两种情况之外,我让调用代码处理可能发生的任何异常。
As others have said, it depends. I tend to use try/catch/finally blocks in two situations:
I need to handle the Exception in some way other than simply re-throwing it.
I need to clean up some resources in the
finally
block.Other than those two situations, I let the calling code handle any Exceptions that might happen.
除了其他人所说的之外,请务必避免这样做:
In addition to what others have said, be sure to avoid doing this:
大多数时候你不应该捕获异常。在某些地方,捕获异常确实有意义,例如,
当您可以从特定异常中恢复时。
当您需要记录或报告它(例如,向用户)时——通常在代码的顶层。
当代码的调用者无法处理异常时,您需要将它们转换为其他错误格式。
此外,using 块语句可用于实际调用 IDisposable 对象上的 Dispose,从而无需 try...finally。
Most of the time you should not be catching exceptions. Some places where it does make sense to catch an exception e.g.,
When you can recover from that specific exception.
When you need to log it or report it (e.g,. to the user)--usually at the top level in your code.
When the caller of your code can not handle exceptions, so you need to convert them into some other error format.
Also, the using block statement can be used to actually call Dispose on IDisposable objects, which removes the need for try...finally.
您期望存储过程出现什么
Exception
?假设您不使用 pokemon 异常处理并准确了解您的应用程序预期执行的操作,任何不符合您想要捕获的特定异常的内容都将被Application
对象捕获。换句话说,不要使用
catch {}
或catch (Exception)
,而是使用专门的异常处理:Application.Error 事件是应该捕获意外行为并且更容易捕获的地方跟踪,而不是简单地让客户返回给您说“我的字段没有显示任何内容”。
What is the
Exception
you are expecting from the stored proc? Providing you don't use pokemon exception handling and know exactly what your application is expected to do, anything that doesn't fit the specific Exception you want to to catch will be caught by theApplication
object.In other words don't use
catch {}
orcatch (Exception)
, but specialized exception handling:The Application.Error event is where unexpected behaviour should be caught and is easier to trace than simply having a customer return to you saying "my fields aren't displaying anything".
在最内层循环中使用“try catch”,当发生特定异常时,该循环应继续执行。请注意,如果您有一个执行 10,000 次的循环,并且在第十次重复时发生异常,但不会影响其他 9,990 次,则捕获异常并让循环继续运行可能会很有用。另一方面,如果异常指示一个错误,表明循环的第 11、12、13 次也将失败,那么让异常终止循环比继续重试操作要快得多那是行不通的。
Use "try catch" within the innermost loop that should keep executing when a particular exception occurs. Be mindful that if you have a loop that executes 10,000 times and an exception occurs on e.g. the tenth repetition which won't affect the other 9,990 it may be useful to catch the exception and let the loop keep going. On the other hand, if the exception indicates a fault that suggests the 11th, 12th, 13th, etc. times through the loop are also going to fail, it would be much faster to let the exception kill the loop than to keep retrying an operation that isn't going to work.