问题
Is it possible to wrap functions inside a try-catch block? It appears to not work for the first function, but would it work for the prototype function declared that way?
Example:
try {
function MyFunction1() {
//function code here
}
MyFunction1.prototype.getValue = function() {
//more code here
}
} catch (e) {
//error handling here
}
回答1:
No, it's not possible to catch an exception in that way.
A try/catch block around a function definition does not catch exceptions thrown from that function.
You need a try/catch block either inside your function, or around the code that's actually calling the function instead.
来源:https://stackoverflow.com/questions/17559379/wrapping-functions-in-try-catch-block-in-javascript