我想从try-catch语句中获取错误对象的详细信息,例如在Servoy JavaScript框架中报告错误的行号和文件(https://servoy.com/),我该如何处理修改下面的代码示例以显示此信息?
var myObj = {'name':'Bob'}
try {
var age = myObj.age; // throws error because myObj has no 'age' property
}
catch (e) {
application.output(e, LOGGINGLEVEL.ERROR); // only outputs message without line number or filename info
}
答案 0 :(得分:0)
Servoy的错误对象包含" lineNumber"和" fileName"属性。它还包含一个" rhinoException"包含具有以下方法的对象的属性" getMessage()"和" getScriptStackTrace()"。
这些属性和消息可用于获取有关错误的详细信息:
var myObj = {'name':'Bob'}
try {
var age = myObj.age; // throws error because myObj has no 'age' property
}
catch (e) {
var iLineNumber = e.lineNumber;
var sFileName = e.fileName;
var sMessage = e.rhinoException.getMessage();
var sStackTrace = e.rhinoException.getScriptStackTrace();
var sMessage = "The following message was detected at line "+iLineNumber+" of the file "+sFileName+". The error message was: "+sMessage+". The full stack trace was: "+sStackTrace;
application.output(sMessage, LOGGINGLEVEL.ERROR);
}