异常处理

发布于 2024-09-28 06:27:10 字数 268 浏览 5 评论 0原文

catch (Exception ex)
{
    DBGlobals.Error("OHMjson.Graph.saveLastGraphName - Error: " + ex.getMessage());
    msg = "Unable to save data";
    status = false;
}

如果我遇到错误,这段代码将抛出错误状态。

相反,我应该抛出 New 异常。 这是正确的方法吗? 我怎样才能以更好的方式处理异常。

catch (Exception ex)
{
    DBGlobals.Error("OHMjson.Graph.saveLastGraphName - Error: " + ex.getMessage());
    msg = "Unable to save data";
    status = false;
}

This piece of code would throw an status as false, if i encounter an error.

Instead should i thrown New exception.
Is this the right way.
How can i handle exception in a better way.

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

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

发布评论

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

评论(2

牵你的手,一向走下去 2024-10-05 06:27:10

更喜欢抛出异常而不是返回错误代码/状态。

返回错误代码意味着调用者应该始终记住检查它。抛出异常允许调用代码决定要做什么(通常决策越高,做出的效果就越好)。

Prefer throwing exceptions to returning error codes/status.

Returning an error code means the caller should always remember to check for it. Throwing the exception allows the calling code to make the decision of what to do (an normally the higher up the decision, the better it can be made).

帅气尐潴 2024-10-05 06:27:10

首先,不要捕获Exception。捕获特定的子类。

其次,是的,底层的 IOException(如下所示)可能应该包装在特定于应用程序的异常中。例如,您可以:

final class DBGlobalsException extends Exception
{
   Field somethingDBGlobalsSpecific;

   //where this is shorthand for chained constructors that look like Exception
   DBGlobalsException(String message, Throwable cause)
   {
     super(message,cause);
   }

   // elaborate your exception with whatever error state you want to propagate outwards
   void setField(Field ...) {}
}

然后您

catch (IOException ex) {
   DBGlobals.Error("OHMjson.Graph.saveLastGraphName - Error: " + ex.getMessage());
   msg = "Unable to save data";
   status = false;
   DBGlobalsException dbe = new DBGlobalsException(msg,ex);
   dbe.setField(status /* or whatever */);
   throw dbe;
}

也可以参见: 抛出异常抛出新的和旧的异常滚动你自己的。这只是其中的一小部分,还有很多关于异常处理、链接等的精彩 SO Q+A。 维基图书

另请阅读Oded的答案并仔细考虑...您的异常应该封装任何状态(错误代码) 、标志等)是外部代码从该条件中恢复所必需的——或者它应该是一个不可恢复的条件(请参阅RuntimeException)。

最后,规范的Effective Java参考:第 43 项:抛出适合抽象的异常

First, don't catch Exception. Catch a specific subclass.

Second, yes it may be that an underlying IOException, which is what this looks like, should be wrapped in an app-specific exception. You could have, for instance:

final class DBGlobalsException extends Exception
{
   Field somethingDBGlobalsSpecific;

   //where this is shorthand for chained constructors that look like Exception
   DBGlobalsException(String message, Throwable cause)
   {
     super(message,cause);
   }

   // elaborate your exception with whatever error state you want to propagate outwards
   void setField(Field ...) {}
}

And you could then

catch (IOException ex) {
   DBGlobals.Error("OHMjson.Graph.saveLastGraphName - Error: " + ex.getMessage());
   msg = "Unable to save data";
   status = false;
   DBGlobalsException dbe = new DBGlobalsException(msg,ex);
   dbe.setField(status /* or whatever */);
   throw dbe;
}

See also: Throwing Exception, Throwing new and old exceptions, Rolling your own. That's just a few, there are a bunch of great SO Q+A's on exception handling, chaining, etc. Some useful best-practices stuff outlined at WikiBooks.

Also read Oded's answer and think it through... your exception should encapsulate whatever status (error codes, flags, etc...) are required for the outer code to recover from the condition -- or it should be an unrecoverable condition (see RuntimeException).

Finally, the canonical Effective Java reference: Item 43: Throw Exceptions Appropriate to the Abstraction.

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