Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Java people frequently consume exceptions silently?

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);         }     } 
like image 599
Sake Avatar asked May 28 '09 15:05

Sake


People also ask

Why do programmers want to handle exceptions?

Many programmers solve this problem by simply ignoring it — errors are reported when their programs crash. Exceptions enable you to write the main flow of your code and to deal with the exceptional cases elsewhere.

What is the point of catching exceptions?

Exceptions are used to provide detailed information about the cause of a particular failure. If you simply let the code fail on its own you miss the opportunity to provide richer details about the actual cause of the failure.


1 Answers

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

"A man gets shot.

He holds his breath and has enough strength to take a bus.

10 miles later the man gets off of the bus, walks a couple of blocks and dies."

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:

"A man gets shot and he dies instantly, and the body lies exactly where the murder just happened."

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

like image 90
OscarRyz Avatar answered Oct 13 '22 05:10

OscarRyz