【发布时间】:2016-04-12 07:51:12
【问题描述】:
我有一点 JS 问题,当我在 $.getJSON 中抛出异常时,父函数没有捕获到这个异常,并且我有一个 JS 错误..
Chrome 的控制台告诉我:
Uncaught [object Object]
(anonymous function) @ ConfLoader.class.js:24
j @ jquery.min.js:2
k.fireWith @ jquery.min.js:2
x @ jquery.min.js:4
(anonymous function) @ jquery.min.js:4
我还有很多其他的 throw 和 try/catch,但我没有这个问题 .. 你能解释一下为什么吗?
这是我的 ConfLoader“类”:
var ConfLoader = {
/* Attributes */
apiToken : null,
lavaBaseURL : null,
apiURL : null,
/* Init method */
load : function(file) {
$.getJSON(file, function(data) {
if (data.apiToken)
this.apiToken = data.apiToken;
else
throw new Exception("Token API (apiToken) not found into config file.");
if (data.lavaBaseURL)
this.lavaBaseURL = data.lavaBaseURL;
else
throw new Exception("Lava URL (lavaBaseURL) not found into config file.");
if (data.apiURL)
this.apiURL = data.apiURL;
else
throw new Exception("API URL (apiURL) not found into config file.");
})
.fail(function() {
throw new Exception("File '"+file+"' not found.");
});
},
/* Methods */
getApiToken : function () {
return this.apiToken;
},
getLavaBaseURL : function () {
return this.lavaBaseURL;
},
getApiURL : function () {
return this.apiURL;
}
}
并且行“throw new Exception("File '"+file+"' not found.");"给我一个 JS 错误.. 但是当我无法 ConfLoader 的方法“加载”时,我在这里捕获了这个异常:
var Powers = {
/* Attributes */
confLoader : null,
/* Methods */
init : function(configFile) {
try {
this.confLoader = ConfLoader.load(configFile);
} catch (e) {
throw e;
}
},
launch : function() {
try {
} catch (e) {
}
}
}
"Exception" 类是一个自定义的 Exception 类,适用于其他 throw :
function Exception(msg, fatal) {
this.message = msg;
this.fatal = fatal;
this.isFatal = function() {
return this.fatal;
}
this.getMessage = function() {
return this.message;
}
return this;
}
谢谢! :)
【问题讨论】:
标签: javascript exception error-handling exception-handling