How to Handle Exceptions in EJB

From the article on DeveloperWorks.

Principles of exception handling

The following are some of the generally accepted principles of exception handling:

1. If you can’t handle an exception, don’t catch it.

2. If you catch an exception, don’t swallow it.

3. Catch an exception as close as possible to its source.

4. Log an exception where you catch it, unless you plan to rethrow it.

5. Structure your methods according to how fine-grained your exception handling must be.

6. Use as many typed exceptions as you need, particularly for application exceptions.

Point 1 is obviously in conflict with Point 3. The practical solution is a trade-off between how close to the source you catch an exception and how far you let it fall before you’ve completely lost the intent or content of the original exception.

Note: These principles are not particular to EJB exception handling, although they are applied throughout the EJB exception-handling mechanisms.

Technorati Tags: ,

How to Format Dates for SQL in Java

Say, you have a date that you need to store in a database record, and the date is entered as a string.

If you are like most beginners in Java, your first attempt will be to parse the string for a Date object by using the static method java.sql.Date.valueOf(String s). However, this will most likely not work as the input date is not in the format that is expected by the this method (i.e. yyyy-MM-dd).

The solution is to use a java.text.SimpleDateFormat object configured with the correct input pattern (e.g. dd/MM/yyyy) to parse the string for a java.util.Date object. The resulting date object can then be represented as a string in the yyyy-MM-dd format, which can be parsed by java.sql.Date.valueOf(String s).

This example demonstrates this.

This code creates a java.sql.Date object from a date value entered as a string by first formatting it to the ISO date standard format.

UPDATE: Of course, there is no need to strictly pass a java.sql.Date object to the java.sql.PreparedStatement.setDate() method as it happily accepts a java.util.Date.

Java Artificial Neural Network

My artificial neural network written in PHP prompted a few requests for a Java implementation. Well, I could only oblige.

Porting the code from PHP was easy; I only had to find and use the Java equivalent of the functions in my original implementation. However, since object serialisation in Java is more complex than in PHP, saving and loading functions were left out.

To understand the concepts and the alorithm, the reader is encouraged to refer to the above-mentioned article. The source code for the ANN class and the example usage can be downloaded.

Technorati Tags: , ,

IM Online Status Indicators

The Yahoo! Messenger status indicator on this page was put together based on information gleaned from a bugtraq advisory and Yahoo! Messenger help page.

It was easier to find information to build the AIM status indicator, perhaps owing to a larger user-base. All that was needed was a search on Google for “aim commands“.

There is no straightforward way to get the status for MSN Messenger, so had to be used. Unfortunately, whenever this page is refreshed, I would be disconnected from MSN.

Technorati Tags: , , ,

PHP collection class

Below is a simple collection class that works approximately like Java collection classes.

The usage should be fairly obvious, but you may also refer to the sample code at the end.


Example usage: