简体   繁体   English

覆盖System.out / err.println

[英]Override System.out/err.println

Since the deprecation of the JDBC Bridge in Java, I've been making use of UCanAccess for JDBC connections. 自从Java中弃用JDBC Bridge以来,我一直在使用UCanAccess进行JDBC连接。 However UCanAccess does not throw exceptions in the form of normal try-catches, but immediately prints error lines to the console. 但是,UCanAccess不会以普通try-catches的形式抛出异常,而是立即将错误行打印到控制台。 As my users will not be accessing the software through the console, I would like to redirect all my console outputs to a JOptionPane message dialog, in order for my users to see error prints instead of it simply going to waste in the non-visible console. 由于我的用户将无法通过控制台访问该软件,因此我想将所有控制台输出重定向到JOptionPane消息对话框,以便用户看到错误打印,而不是简单地浪费在不可见的控制台中。 Is this possible? 这可能吗? I have tried System.setErr , but I can only get to override the write() method, outputting only a int? 我已经尝试过System.setErr ,但是我只能覆盖write()方法,仅输出int吗?

UCanAccess throws exceptions in "the normal form", so your question is a bit misleading and your assumptions are wrong. UCanAccess会以“正常形式”引发异常,因此您的问题有点令人误解,并且您的假设是错误的。

Also it uses SQLWarning while loading the db, see the jdbc interface about it (eg if it can't load a query). 此外,它在加载数据库时使用SQLWarning ,请参见有关它的jdbc接口(例如,如果它无法加载查询)。 The UCanAccess console just prints them. UCanAccess控制台仅打印它们。

At Last, in the case of database corruption , data integrity problems or other very high severity problems jackcess or ucanaccess may print on the System.err. 最后,在数据库损坏的情况下,System.err上可能会打印出数据完整性问题或其他严重性非常高的问题jackcess或ucanaccess。 Obviously it may happen just if they are not blocking despite the severity. 显然,即使严重性没有阻止 ,也可能发生这种情况。 It's just to be sure that error message is logged without blocking a succesful db loading with an Exception(so I can't use a SQLWarning for them ). 只是为了确保在记录错误消息的同时不会阻止带有Exception的成功db加载(因此我不能对它们使用SQLWarning)。

For those specific issues you can redirect the System.err messages to a JOptionPane, but they are often very technical errors about mdb/accdb format, so I think it isn't a good idea show them in a user interface. 对于那些特定的问题,您可以将System.err消息重定向到JOptionPane,但是它们通常是有关mdb / accdb格式的技术性错误,因此我认为在用户界面中显示它们不是一个好主意。

To re-direct the system.err in a specific error file instead(for subsequent problem solving) is much better. 而是将system.err重定向到特定的错误文件中(用于后续的问题解决)要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM