1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 import org.ibex.util.*;
7 /** An exception which can be thrown and caught by JavaScript code */
8 public class JSExn extends Exception {
9 private Vec backtrace = new Vec();
11 public JSExn(String s) { this(JS.S(s)); }
12 public JSExn(JS js) { this(js,null); }
13 public JSExn(JS js, Interpreter cx) { this.js = js; fill(cx); }
15 private void fill(Interpreter cx) {
16 if(cx == null) cx = Interpreter.current();
17 if(cx == null) return;
18 addBacktrace(cx.f.sourceName + ":" + cx.f.line[cx.pc]);
19 cx.stack.backtrace(this);
21 public void printStackTrace() { printStackTrace(System.err); }
22 public void printStackTrace(PrintWriter pw) {
23 for(int i=0; i<backtrace.size(); i++) pw.println(" at " + (String) backtrace.elementAt(i));
24 super.printStackTrace(pw);
26 public void printStackTrace(PrintStream ps) {
27 for(int i=0; i<backtrace.size(); i++) ps.println(" at " + (String) backtrace.elementAt(i));
28 super.printStackTrace(ps);
30 public String toString() { return "JSExn: " + JS.debugToString(js); }
31 public String getMessage() { return toString(); }
32 public JS getObject() { return js; }
34 void addBacktrace(String line) { backtrace.addElement(line); }
36 public static class Wrapper extends RuntimeException {
38 public Wrapper(JSExn e) { this.e = e; }
39 public JSExn refill() {
40 e.addBacktrace("[foreign code]");
46 public static class IO extends JSExn {
47 public IO(java.io.IOException ioe) {
48 super("ibex.io: " + ioe.toString());