Skip to content

Commit ca258da

Browse files
committed
error object defination
1 parent 4c3be1c commit ca258da

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

errorhandling.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,27 @@ setTimeout(function () {
1616
} catch {
1717
console.log("error is caught here!");
1818
}
19-
}, 1000);
19+
}, 1000);
20+
21+
//If an exception happens in “scheduled” code, like in setTimeout, then try...catch won’t catch it
22+
try {
23+
setTimeout(function() {
24+
noSuchVariable; // script will die here
25+
}, 1000);
26+
} catch (err) {
27+
console.log( "won't work" );//that’s because the function itself is executed later, when the engine has already left the try...catch construct.
28+
}
29+
30+
//To catch an exception inside a scheduled function, try...catch must be inside that function:
31+
32+
setTimeout(function() {
33+
try {
34+
noSuchVariable; // try...catch handles the error!
35+
} catch {
36+
console.log( "error is caught here!" );
37+
}
38+
}, 1000);
39+
40+
41+
//Error Object
42+
//When an error occurs, JavaScript generates an object containing the details about it. The object is then passed as an argument to catch

0 commit comments

Comments
 (0)