?
Solved

Need some help writing an error handling class

Posted on 2004-10-21
4
Medium Priority
?
187 Views
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) {      
      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
Comment
Question by:Dawkins
  • 2
  • 2
4 Comments
 
LVL 11

Assisted Solution

by:cjjclifford
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 :-)
0
 
LVL 8

Accepted Solution

by:
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

http://java.sun.com/j2se/1.4.2/docs/api/
0
 
LVL 11

Expert Comment

by:cjjclifford
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...

Cheers,
C.
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12371221
check out his for examples of logging

http://javaalmanac.com/egs/java.util.logging/pkg.html
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month3 days, 19 hours left to enroll

601 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