Hello,
Error objects have several properties:
Code: Select alltry {throw new Error("message") }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
// number: 1
// fileName: (Script4)
// line: 1
// source: try {throw new Error("message") }
// catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
// start: 0
// end: 0
// message: message
// name: Error
// description: message
I can't get why if I create a custom error object which inherits from the Error() object:
Code: Select allfunction MyError(message) {
this.message = message;
}
myError.prototype = new Error();
This one doesn't inherit any property:
Code: Select allfunction MyError(message) {
this.message = message;
}
MyError.prototype = new Error();
try {throw new MyError("Argh!") }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
// message: Argh!
I wonder why. Any suggestion?
Thank you!
Davide Barranca
http://www.davidebarranca.com
Custom Error doesn't inherit properties
-
Mike Hale
Custom Error doesn't inherit properties
I understanding of oop is limited. But what you are trying to do doesn't need a subclass.
Code: Select allvar MyError = function(message){return new Error(message);};// get an instance of Error
MyError.type = "custom";// create a new property and assign value
try {
throw MyError('myCustomErrorMesg');//call the instance
}catch(e) {
for (prop in e) {
$.writeln(prop + ": " + e[prop]);
}
}
Code: Select allvar MyError = function(message){return new Error(message);};// get an instance of Error
MyError.type = "custom";// create a new property and assign value
try {
throw MyError('myCustomErrorMesg');//call the instance
}catch(e) {
for (prop in e) {
$.writeln(prop + ": " + e[prop]);
}
}
-
undavide
Custom Error doesn't inherit properties
Thanks Mike,
as a workaround it... works
(yet the custom error should inherit from Error - this is possibly another Extendscript bug?)
Best
Davide
as a workaround it... works
(yet the custom error should inherit from Error - this is possibly another Extendscript bug?)
Best
Davide
-
Mikaeru
Custom Error doesn't inherit properties
undavide wrote:this is possibly another Extendscript bug?
Another problem I ran into with custom errors, at least up to CS4, is that all error types: Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, appear to be "synonyms" for each other:
Code: Select allvar myError;
myError = new Error ();
alert (myError instanceof Error); // true
alert (myError instanceof ReferenceError); // true (!)
alert (myError instanceof SyntaxError); // true (!)
myError = new ReferenceError ();
alert (myError instanceof Error); // true (!)
alert (myError instanceof ReferenceError); // true
alert (myError instanceof SyntaxError); // true (!)
myError = new SyntaxError ();
alert (myError instanceof Error); // true (!)
alert (myError instanceof ReferenceError); // true (!)
alert (myError instanceof SyntaxError); // true
Another problem I ran into with custom errors, at least up to CS4, is that all error types: Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, appear to be "synonyms" for each other:
Code: Select allvar myError;
myError = new Error ();
alert (myError instanceof Error); // true
alert (myError instanceof ReferenceError); // true (!)
alert (myError instanceof SyntaxError); // true (!)
myError = new ReferenceError ();
alert (myError instanceof Error); // true (!)
alert (myError instanceof ReferenceError); // true
alert (myError instanceof SyntaxError); // true (!)
myError = new SyntaxError ();
alert (myError instanceof Error); // true (!)
alert (myError instanceof ReferenceError); // true (!)
alert (myError instanceof SyntaxError); // true
-
undavide
Custom Error doesn't inherit properties
Thanks for adding extra info Mikaeru!
Is there any hope that Extendscript will undergo some revision (also to keep it consistent with latest ECMA specs)?!
Davide
Is there any hope that Extendscript will undergo some revision (also to keep it consistent with latest ECMA specs)?!
Davide
-
Mike Hale
Custom Error doesn't inherit properties
I could be wrong but it is my understanding that Error, EvalError, RangeError, etc. are values found in the Error.name property. And that property seems to be set by the Error.number property.
Code: Select alltry {throw new Error(15) }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
/* console output
number: 15
fileName: (Script1)
line: 1
source: try {throw new Error(15) }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
start: 0
end: 0
message: Try without catch or finally
name: SyntaxError
description: Try without catch or finally
*/
And you can use the other arguments to override some properties
Code: Select alltry {throw new Error(15,"CustomMessage",25,"[native code]") }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
/* console output
number: 15
fileName: (Script1)
line: 25
source: [native code]
start: 0
end: 0
message: CustomMessage
name: SyntaxError
description: CustomMessage
*/
Code: Select alltry {throw new Error(15) }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
/* console output
number: 15
fileName: (Script1)
line: 1
source: try {throw new Error(15) }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
start: 0
end: 0
message: Try without catch or finally
name: SyntaxError
description: Try without catch or finally
*/
And you can use the other arguments to override some properties
Code: Select alltry {throw new Error(15,"CustomMessage",25,"[native code]") }
catch(e) { for (prop in e) {$.writeln(prop + ": " + e[prop])} }
/* console output
number: 15
fileName: (Script1)
line: 25
source: [native code]
start: 0
end: 0
message: CustomMessage
name: SyntaxError
description: CustomMessage
*/