I never do any serious java coding before, but I learned the syntax, libraries, and concept based on my existing skill (delphi & c#). One think I hardly understand is that I've seen soo many many code that silently consume exception after "printStackTrace" like this
public void process() {
try {
System.out.println("test");
} catch(Exception e) {
e.printStackTrace();
}
}
There are similar code like this one in almost every java articles & projects 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 is belong to the underlying framework (Java Swing for example). There could be some rare situations that the exception should be consumed, but, as I've said, I've seen this thing for an incredible amount of times in java code. Why it looks like a norm to code like this in java world ? I'm puzzled.
Updated:
May I add some more opinion. Based on my background, I'd prefer to remove printStackTrace AT ALL. 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);
}
}