HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Use Exception Chaining to Simplify Debugging"
Volume: 5 Issue: 3, p.14

Download files assoicated with this article

	

Listing 1: The ChainedException Class

// Filename: ChainedException.java
// Author: Barry Mosher

import java.io.PrintStream;
import java.io.PrintWriter;

public class ChainedException extends Exception
{
        private Throwable _previousThrowable = null;
        
        public ChainedException() {}
        
        public ChainedException(String pMsg) {
                super(pMsg);
        }
        
        public ChainedException(Throwable pEx) {
                _previousThrowable = pEx;
        }

        public ChainedException(String pMsg,
                Throwable pEx) {
                super(pMsg);
                _previousThrowable = pEx;
        }
        
        public void printStackTrace() {
                super.printStackTrace();
                if (_previousThrowable != null) {
                        _previousThrowable.printStackTrace();
                }
        }
        
        public void printStackTrace(PrintStream pPS) {
                super.printStackTrace(pPS);
                if (_previousThrowable != null) {
                        _previousThrowable.printStackTrace(pPS);
                }
        }

        public void printStackTrace(PrintWriter pPW) {
                super.printStackTrace(pPW);
                if (_previousThrowable != null) {
                        _previousThrowable.printStackTrace(pPW);
                }
        }
}


Listing 2: Example Use of Exception Chaining

private void registerListeners()
{
        calcBtn.addActionListener(new ActionListener()
        {
                public void actionPerformed(ActionEvent e)
                {
                        try {
                                calculateBonuses();
                        } catch (Throwable ex) {
                                // Handle all exceptions
                                // by displaying a dialog.
                                ExceptionDialog exceptionDialog =
                                        new ExceptionDialog(
                                                BonusCalc.this,ex,
                                                "Calculation Incomplete",true);
                                exceptionDialog.show();
                        }
                }
        });
}

// Simulates a calculation routine that can
// fail and throw exceptions.
private void calculateBonuses()
        throws BonusCalcException {
        try {
                getEmployeeData();
        } catch (EmployeeLookupException ex) {
                // Convert the exception to a higher
                // level of abstraction suitable for this
                // method.
                throw new BonusCalcException(
                        "Failed to get employee data.",ex);
        }
}

// Simulates a query routine that can
// fail and throw exceptions.
private void getEmployeeData()
        throws EmployeeLookupException {
        try {
                Connection con =
                        DriverManager.getConnection(
                        "invalid_connection");
        } catch (SQLException ex) {
                // Convert the exception to a higher
                // level of abstraction suitable for this
                // method.
                throw new EmployeeLookupException(
                        "SQL query failed.",ex);
        }
}




 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.