Most web programmers use a logging framework and handle unexpected errors by reporting them through a static reporting object and forwarding the user to an error page.
My code for reporting an unexpected exception looks something like this:
public static synchronized void report(Throwable e) {
ByteArrayOutputStream logResult = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(logResult));
logger.error(logResult.toString());
}
The logger object is an instance of Category in Log4J
Logging helps me identify problems by examining the logs for errors and figuring out what is happening. The problem this method is that I don't examine the logs until someone reports a problem. It would be really nice to figure out that I have an error before most of my users do. The best way I've found to do is to email the error log to myself using Java Mail API.
I like to send email errors only to myself, because I always find problems that aren't really problems at all (eg: user closed their browser during a request). I've found that sending error emails out to people who aren't used to checking code gets me a gaggle of chicken little helpers in my office that keep me from actually figuring out if the latest exception was a real problem or not.
Another nice trick I like to do is add the machine name in the mail subject. I use the command, "InetAddress.getLocalHost().getHostName()," to get the local host and set up a filter that separates my dev, test and production instances. On a production instance if something goes seriously wrong, then if I'm at my desk, I will learn about the problem before my users and can address it before the any chicken little is any the wiser.