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
 

"Taking Advantage of Low-Level Exceptions"
Vol. 2, Issue 4, p. 42

	

Listing 1: An ExpandableArray class implementation

class ExpandableArray {
    private Object[] aObjs = null;
    private int counter = 0;
    private static final int increment = 10;

    public void add(Object o) {
        do {
            try {
                aObjs[counter++] = o;
                return;
            } catch (NullPointerException npe) {
                // Need to allocate array
                aObjs = new Object[increment];
                counter--;
            } catch (ArrayIndexOutOfBounds aioob) {
                // Need to expand the array
               Object[] ao = new Object[aObjs.length + increment];
                System.arraycopy(aObjs, 0, ao, 0, aObjs.length);
                aObjs = ao;
                counter--;
            }
        } while(true);
    }

    public Object get(int index) {
        try {
            return aObjs[index];
        } catch(Exception e) {
            // Either unallocated array or index illegal
            return null;
        }
    }
}

Listing 2: A BinaryTree class

class BinaryTree {
    Object[] head;

    public void add(Comparable c) {
        try {
            add(c, head);
        } catch (NullPointerException npe) {
            head = new Object[3];
            head[0] = c;
        }
    }

    private void add(Comparable c, Object[] node) {
        if(c.lessThan((Comparable)node[0]))
            try {
                add(c, (Object[])node[1]);
            } catch (NullPointerException npel) {
                node[1] = new Object[3];
                ((Object[])node[1])[0] = c;
            }
        else
            try {
                add(c, (Object[])node[2]);
            } catch (NullPointerException npel) {
                node[2] = new Object[3];
                ((Object[])node[2])[0] = c;
            }
        }
    }
}

Listing 3: New overloaded add() methods for fast unravelling after recursion

public void add(Comparable c) {
    try {
        add(c, head);
    } catch (NullPointerException npe) {
        head = new Object[3];
        head[0] = c;
    } catch (Exception e) {
        // Thrown to unravel the stack
    }
}

private void add(Comparable c, Object[] node)
        throws Exception {
    if(c.lessThan((Comparable)node[0]))
        try {
            add(c, (Object[])node[1]);
        } catch (NullPointerException npel) {
            node[1] = new Object[3];
            ((Object[])node[1])[0] = c;
            throw new Exception();
        }
    else
        try {
            add(c, (Object[])node[2]);
        } catch (NullPointerException npel) {
            node[2] = new Object[3];
            ((Object[])node[2])[0] = c;
            throw new Exception();
        }
    }
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

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.