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

问题描述

我从来没有做过任何严重的Java编码,但是我根据我现有的技能(Delphi& C#)学习了语法,库和概念。我不明白的一件事是,我看到如此多的代码,在 printStackTrace 之后默认使用异常,如下所示:

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

几乎每个Java都有类似的代码文章&项目我遇到了。根据我的知识,这是非常糟糕的。这个例外几乎总是被转发到外部的上下文,如下所示:

  public void process(){
try {
System.out.println(test);
} catch(Exception e){
e.printStackTrace();
抛出新的AssertionError(e);
}
}

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



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

  public void process(){
try {
的System.out.println( 测试);
} catch(Exception e){
throw new AssertionError(e);
}
}


解决方案

一直以为这是类似于以下情况:

当警察得到对身体来说,他们没有发现刚才的线索。他们可能最终会变得更困难。



更好的是:

当警察到达时,所有的证据到位



如果系统失败,最好是



解决问题:


  1. 无知。


      +

  2. 怠速

编辑:



当然,catch部分是有用的。



如果有异常可以完成,那就是应该做的事情。



可能这不是给定代码的例外,可能是预期的东西(在我的类比中,就像一个防弹外套,男人正在等待第一名)。



是的,这个catch可以用于


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();
        }
    }

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);
        }
    }

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.

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:

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.

Better is:

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

If a system is to fail, better is to fail fast

Addressing the question:

  1. Ignorance.
      +
  2. Sloth

EDIT:

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 ).

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

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

09-17 17:34