// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;

public class StackTrace {
    public static void main(String[] args) {
        try {
            throw new Exception("Test1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            throw new Exception("Test2");
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            StackTraceElement[] elm = e.getStackTrace();
            for(int i = 0; i < elm.length; i++)                 {
                System.err.println("        at " + elm[i]);
            }
        }
        try {
            throw new Exception("Test3");
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            System.err.println(sw.getBuffer().toString());
        }
    }
}
