Need some help writing an error handling class

Posted on 2004-10-21
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
    LVL 11

    Assisted Solution

    I suggest you look into Log4J, a very powerful, yet easy to get into, logging framework ( - 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 :-)
    LVL 8

    Accepted Solution

    if u r using JDK 1.4 and above u can also make use of the JDK logging api
    LVL 11

    Expert Comment

    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...

    LVL 8

    Expert Comment

    check out his for examples of logging

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Exchange and Third Party application. 8 23
    java update needs to be set up as msi 1 32
    only14 challenge 19 50
    count7 challenge 12 24
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    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…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now