Not clear about your intent. Complain about the following, and that may give me better understanding. Note test() will ALWAYS report "calling function1" - there is no function1.
<html>
<head>
<script type="text/javascript"">
function test() {
var msg;
try {
msg = "calling function1";
function1();
msg = "function1 passed - calling function2";
function2();
msg = "function2 passed - calling function3";
function3()
msg = "all functions passed";
}
catch (e) {
;// do something useful here
}
return msg;
};
function functiona() {;};
function functionb() {;};
function functionc() {;};
function test2() {
try {
functiona();
functionb();
functionc();
alert("all functions passed");
}
catch (e) {
;// do something useful here
}
};
window.onload = function() {
alert(test());
test2();
};
</script>
</head>
<body>
<div>some content here</div>
</body>
</html>
You could create a function which wraps a function within try catch.
Well, here's an example:
// your own 3 functions functiongood1() { console.log("works"); }
functionbad() { throw"i throw somethuing"; }
functiongood2(someParameter) {
console.log("works with paraemter too.. =>", someParameter);
}
// takes in a function as parameter and executes// within a try catch block. // returns either result or the error.functionrunSafe(fn) {
try {
return fn();
} catch(error) {
console.error(error);
returnnull;
}
}
// execute functions and get all results..var results = [
runSafe(good1),
runSafe(bad),
runSafe(function() { return good2("Yes it does!"); }
];
// log results. // if an element is null, it means it's an error..^^console.log(results);
Not clear about your intent. Complain about the following, and that may give me better understanding. Note test() will ALWAYS report "calling function1" - there is no function1.
<html> <head> <script type="text/javascript""> function test() { var msg; try { msg = "calling function1"; function1(); msg = "function1 passed - calling function2"; function2(); msg = "function2 passed - calling function3"; function3() msg = "all functions passed"; } catch (e) { ;// do something useful here } return msg; }; function functiona() {;}; function functionb() {;}; function functionc() {;}; function test2() { try { functiona(); functionb(); functionc(); alert("all functions passed"); } catch (e) { ;// do something useful here } }; window.onload = function() { alert(test()); test2(); }; </script> </head> <body> <div>some content here</div> </body> </html>