• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Need some help writing an error handling class

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) {      
      ErrorHandler.error(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();
    System.out.println(s);
}

Does this look ok?  Can anyone think of anything extra I should be doing, or have any advice about error logging classes in general?
0
Dawkins
Asked:
Dawkins
  • 2
  • 2
2 Solutions
 
cjjcliffordCommented:
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 :-)
0
 
kiranhkCommented:
if u r using JDK 1.4 and above u can also make use of the JDK logging api

http://java.sun.com/j2se/1.4.2/docs/api/
0
 
cjjcliffordCommented:
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...

Cheers,
C.
0
 
kiranhkCommented:
check out his for examples of logging

http://javaalmanac.com/egs/java.util.logging/pkg.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now