为什么 Java 人经常默默地消费异常? [英] Why do Java people frequently consume exceptions silently?

查看:27
本文介绍了为什么 Java 人经常默默地消费异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前从未进行过任何认真的 Java 编码,但我根据我现有的技能(Delphi 和 C#)学习了语法、库和概念.我几乎不明白的一件事是,我看到了很多代码,它们在 printStackTrace 之后默默地消耗异常,如下所示:

I never did any serious Java coding before, but I learned the syntax, libraries, and concepts based on my existing skills (Delphi & C#). One thing I hardly understand is that I've seen so much code that silently consume exceptions after printStackTrace like this:

    public void process() {
        try {
            System.out.println("test");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

几乎在每篇 Java 文章中都有类似这样的代码 &我遇到的项目.根据我的知识,这是非常糟糕的.异常应该几乎总是像这样转发到外部上下文:

There is similar code like this one in almost every Java article & project I ran into. Based on my knowledge this is very bad. The exception should almost always be forwarded to the outer context like this:

    public void process() {
        try {
            System.out.println("test");
        } catch(Exception e) {
            e.printStackTrace();
            throw new AssertionError(e);
        }
    }

大多数情况下,异常最终应该在属于底层框架(例如 Java Swing)的最外层循环中处理.为什么像这样的代码在 Java 世界中看起来很正常?我很困惑.

Most of the time the exception should end up being handled at the outermost loop which belongs to the underlying framework (Java Swing for example). Why does it look like the norm to code like this in the Java world? I'm puzzled.

根据我的背景,我更愿意完全删除printStackTrace.我会简单地重新抛出一个未处理的又名 RuntimeException(或者更好的是 AssertionError),然后在最合适的位置捕获并记录它:框架最外层循环.>

Based on my background, I'd prefer to remove printStackTrace entirely. I would simply rethrow as an unhandled aka RuntimeException (or, even better, AssertionError), then catch and log it at the most appropriate place: the framework outermost loop.

    public void process() {
        try {
            System.out.println("test");
        } catch(Exception e) {
            throw new AssertionError(e);
        }
    }

推荐答案

我一直认为,类似于下面的场景:

I have always thought, that's similar to the following scenario:

一个人被枪杀了.

他屏住呼吸,有足够的力气坐公共汽车.

10 英里后,该男子下车,走了几个街区后死亡."

当警察到达尸体时,他们对刚刚发生的事情一无所知.他们最终可能有,但要困难得多.

When the police gets to the body, they don't have a clue of what has just happened. They may have eventually but it is much harder.

更好的是:

一个人被枪杀,当场死亡,尸体就在刚刚发生谋杀的地方."

当警察到达时,所有证据都已到位.

When the police arrives, all the evidence is in place.

如果系统要失败,最好快速失败

解决问题:

  1. 无知.
      +
  1. Ignorance.
      +

当然,catch 部分很有用.

Of course, the catch section is useful.

如果某些事情可以在例外情况下完成,那就应该在那里完成.

If something can be done with the exception, that's where it should be done.

可能这不是给定代码的例外,可能是预期的东西(在我的比喻中就像一件防弹衣,而那个人首先在等待射击).

Probably that is NOT an exception for the given code, probably it is something that is expected ( and in my analogy is like a bulletproof jacket, and the man was waiting for the shot in first place ).

是的,catch 可用于 抛出适合抽象的异常

And yes, the catch could be used to Throw exceptions appropriate to the abstraction

这篇关于为什么 Java 人经常默默地消费异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆