1 //jls.loader.provide('jls.lang.Exception'); 2 3 jls.lang.Exception = jls.lang.Class.create(/** @lends jls.lang.Exception.prototype */ 4 { 5 /** 6 * @param {String} [message] The detail message. 7 * @param {Object} [cause] The cause. 8 * @constructs 9 * @class Provide the base Exception. 10 */ 11 initialize : function(message, cause, name) { 12 /** 13 * The detail message 14 * 15 * @private 16 * @type String 17 */ 18 this._message = message || null; 19 /** 20 * The cause 21 * 22 * @private 23 * @type Object 24 */ 25 this._cause = null; 26 if (cause) { 27 this._cause = jls.lang.Exception.wrap(cause); 28 } 29 this._name = name || null; 30 }, 31 /** 32 * Returns the cause of this exception or null if the cause is nonexistent or unknown. The cause is the exception that caused this 33 * exception to get thrown. 34 * 35 * @returns {Object} The cause. 36 */ 37 getCause : function() { 38 return this._cause; 39 }, 40 /** 41 * Returns the detail message string of this exception. 42 * 43 * @returns {String} The detail message. 44 */ 45 getMessage : function() { 46 return this._message; 47 }, 48 getName : function() { 49 return this._name || jls.loader.getClassname(this) || 'jls.lang.Exception'; 50 }, 51 /** 52 * Returns the stack trace of this exception. 53 * 54 * @returns {Array} The stack trace. 55 */ 56 getStackTrace : function() { 57 return []; 58 }, 59 /** 60 * Prints this exception and its stacktrace. 61 * 62 * @param {jls.io.PrintStream} [ps] The print stream to use. 63 */ 64 printStackTrace : function(ps) { 65 if (! ps) { 66 return; 67 } 68 var last = null; 69 for (var e = this; e != null; e = e.getCause()) { 70 if (e == this) { 71 ps.println(e.toString()); 72 } else { 73 ps.println('Caused by: ' + e.toString()); 74 } 75 var st = e.getStackTrace(); 76 var steStr = null; 77 for (var i = 0; i < st.length; i++) { 78 var ste = st[i] 79 steStr = '\tat ' + ste.fileName + ':' + ste.lineNumber; 80 if (ste.arguments && (ste.arguments.length > 2)) { 81 steStr += ' called with ' + ste.arguments; 82 } 83 if (steStr == last) { 84 ps.println('\t... ' + (st.length - i) + ' more'); 85 break; 86 } 87 ps.println(steStr); 88 } 89 last = steStr; 90 } 91 }, 92 /** 93 * Returns a short description of this exception. 94 * 95 * @returns {String} The detail message. 96 */ 97 toString : function() { 98 var msg = this.getMessage(); 99 if (msg == null) { 100 return this.getName(); 101 } else { 102 return this.getName() + ': ' + msg; 103 } 104 } 105 }); 106 107 Object.extend(jls.lang.Exception, /** @lends jls.lang.Exception */ 108 { 109 wrap : function(e) { 110 if (e instanceof jls.lang.Exception) { 111 return e; 112 } else { 113 return new jls.lang.Exception(e == null ? undefined : e.toString()); 114 } 115 } 116 }); 117 118