4 ms·
Java is old enough that the current best practices recommend using the language and libraries differently than originally intended. For new developers that wan
by rickmode 13y ago
Java is old enough that the current best practices recommend using the language and libraries differently than originally intended.
For new developers that want to do more than trivial things in Java, reading "Effective Java" will help you avoid many of the pitfalls in the language.
As an example, the current best practice around exceptions is that checked exceptions were a mistake. Don't use them in new code. For code that must deal with checked exceptions, you can create your own general exception class derived from RuntimeException, then use a try/catch block to throw your own runtime exception that wraps the checked exception. (But do this smartly; for cases like IOException with web stuff, it is sometimes easier to add "throws IOException" to all your related methods than to exhaustively catch and rethrow.)
As a second example, do not use the JDBC library directly. It is very poorly designed and direct usage can very easily lead to resource leaks. Use something like Spring's JDBC templates or roll your own. (I generally find Spring to be a bloated mess, but the JDBC template stuff is very useful.)
But perhaps the best bet is to use a more modern JVM-based language (of which my favorite is Clojure), though use of these alternatives on Android may be problematic.
- Sandman 13y agoI'm really, really sorry that most of today's developers (it seems) consider checked exceptions to be a bad practice. Sometimes I feel like I'm the only person in the world still advocating their usage. I'm currently working on an application that uses libraries developed by people who had the same opinion about checked exceptions as you. Of course, their code still throws exceptions. Except now, I don't know whether a method I call can throw an exception and whether I should write code to handle it until it all blows up at runtime (more often than not - in production). So what do I do then? I write a try-catch to handle their exception. The exact same thing I would have done had they decided to throw a checked exception. Only now, I don't get any warning that I should handle possible exceptional behavior at compile time, I get it at some point during runtime.
- Teckla 13y agoI'm really, really sorry that most of today's developers (it seems) consider checked exceptions to be a bad practice. Sometimes I feel like I'm the only person in the world still advocating their usage. You're not alone. I find checked exceptions to be an invaluable language feature.