1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 import org.ibex.util.*;
10 /** An exception which can be thrown and caught by JavaScript code */
11 public class JSExn extends Exception {
12 private Basket.List backtrace = new Basket.Array();
14 public JSExn(String s) { this(JSU.S(s)); }
15 public JSExn(JS js) { this(js,null); }
16 public JSExn(JS js, Interpreter cx) { this.js = js; fill(cx); }
18 private void fill(Interpreter cx) {
19 if(cx == null) cx = Interpreter.current();
20 if(cx == null) return;
21 addBacktrace(cx.f.sourceName + ":" + cx.f.line[cx.pc]);
22 cx.stack.backtrace(this);
24 public void printStackTrace() { printStackTrace(System.err); }
25 public void printStackTrace(PrintWriter pw) {
26 for(int i=0; i<backtrace.size(); i++) pw.println(" at " + (String) backtrace.get(i));
27 super.printStackTrace(pw);
29 public void printStackTrace(PrintStream ps) {
30 for(int i=0; i<backtrace.size(); i++) ps.println(" at " + (String) backtrace.get(i));
31 super.printStackTrace(ps);
33 public String toString() { return "JSExn: " + JSU.str(js); }
34 public String getMessage() { return toString(); }
35 public JS getObject() { return js; }
37 void addBacktrace(String line) { backtrace.add(line); }
39 public static class Wrapper extends RuntimeException {
41 public Wrapper(JSExn e) { this.e = e; }
42 public JSExn unwrap() {
43 e.addBacktrace("[foreign code]");
49 public static class IO extends JSExn {
50 public IO(java.io.IOException ioe) {
51 super("ibex.io: " + ioe.toString());