Okay, so I’ve noticed, going through some programs other people have written (for education of myself). And I have noticed, why do people sometimes not have an output for their exceptions? Like they’ll just do a
public class noException {
public static void main(String[] args) {
try {
System.out.println("Hello World!");
} catch(Exception e) {
}
}
}
Wouldn’t you want to see what exception has been caught? Or does it come down to just personal preference with programmers?
Yes, this is a bad practice mainly by two reasons. The first one is that by catching Exception you are catching any kind of them, whereas you should catch all possible exceptions individually, each one in its own catch (it is completely legal to do it for one try). By catching the generic you can offuscate another unexpected exceptions in your code and makes it much more difficult to debug.
The other reason is that you should do something once you have catched the exception, at least do some log, show an error message, rollback, etc. And if you definitely want to do nothing you should provide a comment between the catch brackets explaining the reason why you pass.