如何在 javascript 中进行正确的错误处理?
How to do proper error handling in javascript?
我有三个函数,每个函数都调用另一个函数。
function one() {
two();
}
function two() {
three();
}
function three() {
// here an error will occur, and then should be resolved
try {
// whatever
} catch (e) {
// here should the error be resolved;
}
}
现在我的问题是,我如何实现调用 one() 时,three() 中发生的错误将传递给函数 two(),然后传递给函数 one(),然后函数 one() 执行一些操作错误只有这个功能可以。我用回调尝试了它,但没有用 try/catch,但由于我对所有错误解决的东西都不熟悉,所以我可能搞砸了。在这里我尝试回调并尝试捕获:
function one() {
try {
two();
} catch (e) {
console.log(e);
}
}
function two() {
try {
three();
} catch (e) {
return e;
}
}
function three() {
// here an error will occur, and then should be resolved
try {
// whatever
} catch (e) {
return new Error("ERROR");
}
}
回调:
function one(function (err) {
if (err == "ERROR") {
console.log("everything worked");
} else { throw err }
});
function two(callback) {
try {
three(callback);
} catch (e) {
return callback(e);
}
function three(callback) {
try {
//whatever
} catch (e) {
return callback(new Error("ERROR");
}
}
也许我应该解释一下我在做什么。我有一个函数可以通过文件名从文件中读取数据,然后 returns 数据。另一个函数接收数据并对其进行操作,但我只关注接收。那也有参数路径。基本上,它看起来像这样:
function main() {
try {
manipulateData("C:\......");//That file doesnt exist, so an error will occur
} catch (e) {
return console.log("Error": e);
}
}
function manipulateData(path) {
var data = getUserdata(path);
.....
}
function getUserdata(path) {
return fs.readFileSync(path);
}
要处理 one
中的错误,您在 one
中使用 try
/catch
:
function one() {
console.log(`in one`);
try {
two();
} catch (e) {
console.log(`handling error "${e.message}...`);
console.log("continuing work...");
}
}
function two() {
console.log(`in two`);
three();
}
function three() {
console.log(`in three`);
// Error is caused here:
null.example();
}
one();
我有三个函数,每个函数都调用另一个函数。
function one() {
two();
}
function two() {
three();
}
function three() {
// here an error will occur, and then should be resolved
try {
// whatever
} catch (e) {
// here should the error be resolved;
}
}
现在我的问题是,我如何实现调用 one() 时,three() 中发生的错误将传递给函数 two(),然后传递给函数 one(),然后函数 one() 执行一些操作错误只有这个功能可以。我用回调尝试了它,但没有用 try/catch,但由于我对所有错误解决的东西都不熟悉,所以我可能搞砸了。在这里我尝试回调并尝试捕获:
function one() {
try {
two();
} catch (e) {
console.log(e);
}
}
function two() {
try {
three();
} catch (e) {
return e;
}
}
function three() {
// here an error will occur, and then should be resolved
try {
// whatever
} catch (e) {
return new Error("ERROR");
}
}
回调:
function one(function (err) {
if (err == "ERROR") {
console.log("everything worked");
} else { throw err }
});
function two(callback) {
try {
three(callback);
} catch (e) {
return callback(e);
}
function three(callback) {
try {
//whatever
} catch (e) {
return callback(new Error("ERROR");
}
}
也许我应该解释一下我在做什么。我有一个函数可以通过文件名从文件中读取数据,然后 returns 数据。另一个函数接收数据并对其进行操作,但我只关注接收。那也有参数路径。基本上,它看起来像这样:
function main() {
try {
manipulateData("C:\......");//That file doesnt exist, so an error will occur
} catch (e) {
return console.log("Error": e);
}
}
function manipulateData(path) {
var data = getUserdata(path);
.....
}
function getUserdata(path) {
return fs.readFileSync(path);
}
要处理 one
中的错误,您在 one
中使用 try
/catch
:
function one() {
console.log(`in one`);
try {
two();
} catch (e) {
console.log(`handling error "${e.message}...`);
console.log("continuing work...");
}
}
function two() {
console.log(`in two`);
three();
}
function three() {
console.log(`in three`);
// Error is caused here:
null.example();
}
one();