Tuesday, July 03, 2012

Tip of the Day: Error Handling Code Review

Error Handling

Here are some general best practices to consider around error handling code review.

These tips are by no means definitive, but serve as reminders to developers what they should consider in a checklist.

Note: error handling and general code reviews are really not separate processes.
  • Invalid parameter values are handled properly early in methods (Fast Fail).
  • NullPointerException conditions from method invocations are checked.
  • Consider using a general error handler to handle known error conditions.
  • An Error handler must clean up state and resources no matter where an error occurs.
  • Avoid using RuntimeException, or sub-classes to avoid making code changes to implement correct error handling.
  • Define and create custom Exception sub-classes to match your specific exception conditions. Document the exception in detail with example conditions so the developer understands the conditions for the exception.
  • (JDK 7+) Use try-with-resources. (JDK < 7) check to make sure resources are closed.
  • Don't pass the buck! Don't create classes which throw Exception rather than dealing with exception condition.
  • Don't swallow exceptions! For example catch(Exception ignored) {}. It should at least log the exception.

Monday, July 02, 2012

Typed Query RESTful Service Example

This example demonstrates how to use a TypedQuery<T> in a JAX-RS (Jersey) application. The service implementation utilizes a @Stateless session bean to handle the transactions, and scale the application. The application was developed using NetBeans, and GlassFish 3.1.2.

The primary reason for using a TypedQuery<T> is to maintain type safety, and give the compiler a chance to determine if there are any problems with the code. This combined with a CriteriaQuery<T> provide additional safety and functionality that was not available in Java EE5 and JPA 1.0.

There is additional functionality in the application. There is an index page which leads to the examples.

The project can be downloaded here: TypedQueryExample.zip

Interesting Bits...


JavaFX 2.2 Pie Chart with JPA 2.0

Shipping Costs By Purchase Order
I saw an example of a Pie Chart using JavaFX. I thought that it looked really nice and I wondered how easy it would be to do using JPA 2.0, an example Apache Derby DB included in NetBeans, and NetBeans 7.2.

Well the first iteration took about 10 minutes, and the tweaking took another 15 minutes. Basically, if you asked me to do it again, I bet I could do it in under 5 minutes. My output looks really cool too.

I saw an old post using JavaFXScript to create a similar output which was used in JSF. I am wondering if I can do the same thing using version 2.2 of JavaFX, or if I should wait until the HTML libraries are complete.

The code for the project can be downloaded here: PieChartExample.zip

PieChartExample.java


Popular Posts