// File:  ADTList\ListException.java

/**
 * Realization of a List ADT
 * 
 * @author Bary W Pollack
 * @version Dec. 31, 2000 - 23:00 PST
 */

import java.io.*;

// An Exception class specialized for use by the List methods

// If you extend RuntimeException, then you are NOT forced to
// place each method call within a try-catch block.  Of course,
// in this case an UNCAUGHT exception will cause your program
// to terminate -- but, that's probably what you want to do

// If you extend Exception, instead, then you MUST place each
// method call within a try-catch block

public class ListException extends RuntimeException {

    // Default ListException
    ListException() {
        super();
    }

    // ListException just displaying normal call-stack backtrace
    ListException(String sMessage, int nUnused) {
        super(sMessage);
        StringWriter sw = new StringWriter();
        new Throwable().printStackTrace(new PrintWriter(sw));
        String callStack = sw.toString();
        int atPos  = callStack.indexOf("at ");
        atPos = callStack.indexOf("at ", atPos+1);
        System.out.println();
        System.out.println("******************************");
        System.out.print(callStack.substring(atPos-1));
    }

    // ListException displaying the call-stack backtrace in a RealJ-ready form
    ListException(String sMessage) {
        super(sMessage);
        StringWriter sw = new StringWriter();
        new Throwable().printStackTrace(new PrintWriter(sw));
        String callStack = sw.toString();
        int atPos  = callStack.indexOf("at ");
        System.out.println();
        System.out.println("******************** BACKTRACE ********************");
        String cs = callStack.substring(atPos-1);
        while (cs.length() > 12) {
            int lPos = cs.indexOf("(");
            int rPos = cs.indexOf(")");
            System.out.print(cs.substring(lPos+1, rPos) + "  from ");
            atPos = cs.indexOf("at ");
            System.out.println(cs.substring(atPos+3, lPos+1) + ")");
            cs = cs.substring(rPos+1);
        }
        System.out.println("******************** EXCEPTION ********************");
    }

}
