如何以及在何处捕获 j2ee 应用程序中的异常

发布于 2024-07-23 11:39:52 字数 1021 浏览 3 评论 0原文

好的,我在一个 j2ee 项目中工作,该项目在存储库中有 2 个分支,我被命令将它们混合在一起。

我正在编码,然后 Netbeans 询问我“未报告的异常 blah bla bla 必须被捕获或声明为抛出”,并让我选择是处理每个异常还是直接抛出它希望其他人捕获。

我正在使用的类是这些:

DataBase - DataObject - PersonDB(我在这里工作)

DataBase 是 DBMS 的抽象(支持其中几个)

DataObject 只是 CRUD,DBMS 和 java 之间的类型转换,以及一些反映通用性的东西,它使用Database作为成员变量

PersonDB 是表中名为person的字段到java类型的映射,这个类扩展了DataObject

现在在版本1中(只是名字实际上并行工作)捕获所有产生异常的地方,例如在 DataBase 类中

try {
        Class.forName(this.driver);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(BD.class.getName()).log(Level.SEVERE, null, ex);
    }

或在 DataObject 类中捕获: SQLException、NoSuchFieldException、IllegalArgumentException

现在在 版本 2 上,所有这些都留给向上调用者,如下所示:

public BD (String Adriver, String Ahost, String Abase, String Alogin, String Apassword) 
throws java.lang.ClassNotFoundException { ... }

这是解决此类问题的最佳方法,特别是如果我使用 struts

我为我的英语道歉

ok, im working in a j2ee project that has 2 branches in the repo and i'm ordered to mix them.

i was coding and then netbeans ask me "unreported exception blah bla bla must be caugth or declared to be thrown" and gives me the choice of just handle each exception or just throw it hoping someone else catches.

The classes i'm working with are these:

DataBase - DataObject - PersonDB(I'm working here)

DataBase an abstraction of the DBMS(supports a couple of them)

DataObject is just the CRUD, type conversion between the DBMS and java , and some reflection things for generality, it uses Database as a member variable

PersonDB is a map of the fields in the table called person to java types, this class extends DataObject

Now in the version 1(just the name actually worked in parallel) catch all the exceptions where they are produced for example in the class DataBase:

try {
        Class.forName(this.driver);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(BD.class.getName()).log(Level.SEVERE, null, ex);
    }

or in the DataObject class catching:
SQLException, NoSuchFieldException, IllegalArgumentException

now on version 2 all that is left to the up caller like this:

public BD (String Adriver, String Ahost, String Abase, String Alogin, String Apassword) 
throws java.lang.ClassNotFoundException { ... }

which is the best way to go in your oppinion in this kind of issues, specially if i'm using struts

I apologize for my English

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

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

发布评论

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

评论(2

不知在何时 2024-07-30 11:39:52

我要问的第一个问题是:如果这是一个 J2EE 应用程序,那么您要手动加载 JDBC 驱动程序做什么? 这就是数据源的用途。

其次,如果你确实需要点他的,那么问问自己:这个异常发生的结果是什么? 它可恢复吗? 或者故障是否如此灾难性以至于您的应用程序无法运行?

如果情况如此灾难性,您的应用程序无法运行,请执行以下操作:

try {
  ...
} catch (SomeCheckedException e) {
  throw new RuntimeException(e);
}

使用“抛出...”子句来污染您的接口是没有意义的。

或者,如果它是可恢复的或可能可恢复的,那么您确实需要更好地处理它。 但很难给出答案。 确实这取决于具体情况。

例如,如果您以这种方式加载模块/插件,您只需记录插件 XYZ 无法加载(记录异常)并继续。 如果这是用户操作的直接结果,您需要以某种方式向用户报告操作失败(并记录错误)等。

Well the first question I have to ask is: if this is a J2EE application, what are you doing manually loading JDBC drivers? This is what data sources are for.

Secondly, if you do need to dot his then ask yourself this: what is the result of this exception happening? Is it recoverable? Or is the failure so catastrophic your application can't run?

If it's so catastrophic your application can't run do this:

try {
  ...
} catch (SomeCheckedException e) {
  throw new RuntimeException(e);
}

There is no point polluting your interfaces with "throws ..." clauses.

Alternatively if it is recoverable or potentially recoverable then you do need to handle it more nicely. It's hard to give an answer as to how though. Really it depends on the circumstances.

For example, if you're loading modules/plugins this way, you just log that plugin XYZ could not be loaded (logging the exception) and move on. If this is the direct result of a user action you need to somehow report to the user that the action failed (and also log the error), etc.

北笙凉宸 2024-07-30 11:39:52

异常处理始终是一个“我可以处理吗?”的问题。 - 其中句柄不仅仅意味着记录和重新抛出。

有时,仅仅为了抛出其他抽象级别的异常就值得捕获(“我可以为调用者生成更清晰的错误吗?”)。

在这两种情况下,您都必须考虑是否传递原因(“它对调用者有有用的信息吗?”)-不仅仅是随时这样做,您将获得大量无用的日志文件。 当捕获异常时,您通常会记录捕获的异常,可能仅使用调试级别,但在调试客户系统的情况下,良好的日志信息通常是“调试”系统的唯一机会。

异常处理和日志记录通常做得不好。 但对于产品或长期项目来说,这将是一项不错的投资。

Exception handling is always a question of "Can i handle it?" - where handle means more than log and rethrow.

Sometimes it is worth to catch just to throw an exception of an other abstraction level ("Can i produce a more clear error for the caller?").

In both cases you have to think about passing the cause or not ("Has it useful informaton for the caller?") - not just do it any time, you will get tons of useless log files. When catching an exception, you would normally log the catched exception, maybe with debug level only, but in case of debugging a customers system, good log information is often the only chance to "debug" the system.

Exception handling and logging is often not done well. But for a product or longtime project it would be a good investment.

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