Go Premium for a chance to win a PS4. Enter to Win


Need some help writing an error handling class

Posted on 2004-10-21
Medium Priority
Last Modified: 2010-03-31
Hi all,

I'm teaching myself Java by writing an application.  I've decided to add an error logging class to my project but I'm making it up as I go along, so I'd appretiate some advice!  

At the moment my idea is to add try/catch blocks to my code, which throw custom Exceptions when I think an error has occured, like this:

    myObject returnObject = null;
    try {
        ..... do some code to attempt to assign a value to returnObject
        if(returnObject == null) throw new Exception();
    catch (Exception e) {      
      returnValue = assignADefaultValue();

// In the error handler class..
public static void error(Exception e) {
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw, true));
    String s = sw.toString();

Does this look ok?  Can anyone think of anything extra I should be doing, or have any advice about error logging classes in general?
Question by:Dawkins
  • 2
  • 2
LVL 11

Assisted Solution

cjjclifford earned 240 total points
ID: 12371143
I suggest you look into Log4J, a very powerful, yet easy to get into, logging framework (http://logging.apache.org/log4j/docs/) - with this you setup a Logger, and then can use this Logger to log the exception...

class MyClass {
    static Logger log = Logger.getLogger( MyClass.class );
    public void test() {
        try {
            throw new Exception( "Test exception..." );
        catch( Exception e ) {
            log.error( "Caught the exception", e );

Then using Log4J configuration, many different ways of outputting this log can be done, even all the way to routing directly to SNMP traps, etc (generally though simply out to a log file is sufficient :-)

Accepted Solution

kiranhk earned 260 total points
ID: 12371181
if u r using JDK 1.4 and above u can also make use of the JDK logging api

LVL 11

Expert Comment

ID: 12371202
Also, handling an error will have different meanings for most situations... some might just be handled by logging, and returning default values (like your example), some might cause a fair lot of work to be done (e.g. in a loosly connected system checking a connection may cause an exception (connection down) which may prompt the system to re-connect, etc.)

My point is that developing generic handlers is not really feasible, apart from developing generic ways of outputting/reporting errors, etc.

If your using JDK1.4+ then there is also a logging API provided in the JDK - the API is the same as that of log4J (log4J migrated over the the Sun API for this reason) so you can migrate an application from one to the other with relative ease...


Expert Comment

ID: 12371221
check out his for examples of logging


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question