为什么 Java 人经常默默消费异常?

Why do Java people frequently consume exceptions silently?(为什么 Java 人经常默默消费异常?)

本文介绍了为什么 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 人经常默默消费异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么 Java 人经常默默消费异常?

基础教程推荐