抛出时自定义 Java 异常打印

发布于 2024-08-15 09:51:57 字数 85 浏览 5 评论 0原文

我想设计它,以便每当抛出我的自定义异常之一时,它都会自动将堆栈跟踪打印到文件中。有没有一种方法可以重写来实现此目的?这样做将有助于减少我的项目中的大量代码。

I want to design it such that whenever one of my custom exceptions is thrown, it automatically prints the stacktrace to a file. Is there a method I can override to accomplish this? Doing this would help to reduce a noticable amount of code in my project.

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

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

发布评论

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

评论(5

心头的小情儿 2024-08-22 09:51:57

您可以让自定义异常继承自 RuntimeException,然后设置 UncaughtExceptionHandler 在相关线程上查找异常并按照您喜欢的方式处理它们。

You can have your custom exceptions inherit from RuntimeException, then set the UncaughtExceptionHandler on the relevant Threads to look for your exceptions and handle them however you like.

孤星 2024-08-22 09:51:57

一旦您调用异常的构造函数,堆栈跟踪就可用。您无法对抛出的事件做出反应,但可以在构造函数中编写堆栈跟踪。
如果您有一个公共异常类,它是所有自定义异常的基础,那么您可以在其构造函数中完成所有这些操作。

The stacktrace is available as soon as you call the constructor of your exception. You can't react to the event of being thrown, but you can write the stacktrace inside your constructor.
If you have a common exception class that's the base of all your custom exceptions then you could do all this in its constructor.

要走干脆点 2024-08-22 09:51:57

我可以重写一个方法来完成此任务吗?

是的,printStacktrace() 方法。

您可以为异常创建一个基类,它们会调用“内部”打印,该打印将被重定向到您的文件。

您可以使用 Logger 并让该特定记录器指向您想要的文件(并在需要时更改它、禁用它、重新启用它等),

大致如下:

class MyStackTrace extends Throwable {
     public void printStacktrace() {
         super.printStracTrace();
         internalPrint();
     }
     private void internalPrint() {
         StringWriter sw = new StringWriter();
         printStackTrace( sw );
         Logger logger = Logger.getLogger("exceptions");
         logger.warning( sw.toString() );
     }
 }

Is there a method I can override to accomplish this?

Yes, the printStacktrace() method.

You can create a base class for your exceptions and them call to an "internal" print that would be redeirected to your file.

You can use a Logger and have that specific logger pointing to the file you desire ( and change it, disable it , re-enable it, etc when you need to )

Something along the lines:

class MyStackTrace extends Throwable {
     public void printStacktrace() {
         super.printStracTrace();
         internalPrint();
     }
     private void internalPrint() {
         StringWriter sw = new StringWriter();
         printStackTrace( sw );
         Logger logger = Logger.getLogger("exceptions");
         logger.warning( sw.toString() );
     }
 }
迷路的信 2024-08-22 09:51:57

我可以帮助您在引发异常时打印堆栈跟踪。但构造异常时很容易做到 - 只需在自定义异常的构造函数中包含 printStackTrace() 即可。

I can'r help you print a stack trace when an exception is thrown. But it's easy enough to do when the exception is constructed - Just include the printStackTrace() in your custom exception's constructor.

神也荒唐 2024-08-22 09:51:57

一般来说,记录每个异常创建并不是一个好主意。捕手应该真正决定什么是异常的最佳处理。此外,重写异常中的方法并记录到文件会破坏围绕异常的一般约定。

顺便说一句,您可能会发现一些与后期日志记录相关的可怕性能问题。鉴于覆盖发生在中心位置,您将很难解决此问题。

如果您仍然想在抛出异常时进行记录,那么更好的解决方案是使用/创建像 Throwables 这样的实用程序类。将代码调用为 Throwables.logAndThrow(new CustomerException(...)),同样的一行代码,但从长远来看是灵活的。 logAndThrow 可以像使用上一张海报的记录器技术一样简单:

公共静态无效logAndThrow(Throwable t){
记录器.警告(t);
扔 t;
}

In general, this is not a great idea to log on every exception creation. The catcher should really decide what is the best handling of an exception. Also overriding the method in exception and logging to a file breaks the general contract around exception.

On a side note, you may discover some horrible performance problem related to logging at later stage. Given overriding happens in a central place you will have hard time fixing this.

If you still want to log while throwing the exception then the better solution is to use/create a utility class like Throwables. Call the code as Throwables.logAndThrow(new CustomerException(...)), same one line of code but flexible for the long term. logAndThrow could be as simple as, using the logger technique of previous poster:

public static void logAndThrow(Throwable t) {
logger.warning(t);
throw t;
}

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