在 try catch 块中抛出异常

发布于 2024-09-11 00:52:37 字数 1700 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

预谋 2024-09-18 00:52:37

catch 将捕获您的异常(以及发生的任何其他异常)。话虽如此,我尽量避免编写这样的代码。

就我个人而言,我认为没有理由对同一范围内抛出的异常进行异常处理(catch)。如果您可以在方法中处理错误 - 将异常处理(即:日志记录)也直接放在 try 块中。

在我看来,使用catch对于捕获try块中的方法抛出的异常更有用。例如,如果您的 // do stuff 部分碰巧调用了引发异常的方法,这会更有用。

另外,我建议不要捕获每个异常 (Exception e),而是捕获您可以正确处理的特定类型的异常。唯一的例外是,如果您在 catch 中重新抛出异常,即:将其用于日志记录目的,但仍让它在调用堆栈中冒泡。

The catch will catch your exception (and any other that occurs). That being said, I try to avoid writing code like this when possible.

Personally, I see little reason to ever have exception handling (catch) for an exception thrown in the same scope. If you can handle your error in your method - put the exception handling (ie: logging) directly in the try block as well.

Using a catch is more useful, IMO, for catching exceptions thrown by methods within your try block. This would be more useful, for example, if your // do stuff section happened to call a method that raised an exception.

Also, I recommend not catching every exception (Exception e), but rather the specific types of exceptions you can handle correctly. The one exception to this would be if you're rethrowing the exception within your catch - ie: using it for logging purposes but still letting it bubble up the call stack.

神魇的王 2024-09-18 00:52:37

是的,它会捕获从 Exception 派生的 ApplicationException

在大多数情况下,处理基本异常应该没问题,除非您需要记录或使用不同类型的异常执行某些操作...

try {
    if (isFileDownloaded)
       doSomeThings();
    else
       throw new ApplicationException("Something expectedly unexpected happened.");
}
catch(ApplicationException e)
{
   // log application exception here...
}
catch(Exception e)
{
   // log all other exceptions here...
}
finally
{
   // release resources...
}

Yes, it will catch ApplicationException as it derives from Exception.

Handling the base exception should be fine in most cases unless you need to log or do something with a different type of exception...

try {
    if (isFileDownloaded)
       doSomeThings();
    else
       throw new ApplicationException("Something expectedly unexpected happened.");
}
catch(ApplicationException e)
{
   // log application exception here...
}
catch(Exception e)
{
   // log all other exceptions here...
}
finally
{
   // release resources...
}
遗弃M 2024-09-18 00:52:37

另外,仅供参考,ApplicationException自 .NET 2.0 起作为派生例外已被弃用。它从来没有打算作为一个异常来单独抛出,所以你可能根本不应该使用它。

Also, FYI, ApplicationException has been deprecated since .NET 2.0 as an exception to derive from. It was never intended as an exception to throw on its own, so you probably shouldn't use it at all.

情释 2024-09-18 00:52:37

是的,catch 会捕获您的 ApplicationException ,是的,这是糟糕的编码风格。作为一个好的一般规则,仅捕获特定的异常以及您将要处理的异常,例如修复应用程序状态。

Yes the catch would catch your ApplicationException and yes is is poor coding style. As a good general rule only catch specific exception and those that you are going to do something with, like fixing up application state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文