1try {
2 throw "I'm Md Abdur Rakib"
3 console.log("You'll never reach to me", 123465)
4} catch (e) {
5 console.log(e); // I'm Md Abdur Rakib
6}
1FactoryController.prototype.create = function (callback) {
2 //The throw is working, and the exception is returned.
3 throw new Error('An error occurred'); //outside callback
4 try {
5 this.check(function (check_result) {
6 callback(check_result);
7 });
8 } catch (ex) {
9 throw new Error(ex.toString());
10 }
11}
12
13FactoryController.prototype.create = function (callback) {
14 try {
15 this.check(function (check_result) {
16 //The throw is not working on this case to return the exception to the caller(parent)
17 throw new Error('An error occurred'); //inside callback
18 });
19 } catch (ex) {
20 throw new Error(ex.toString());
21 }
22}
1try {
2 throw new Error('Whoops!')
3} catch (e) {
4 console.error(e.name + ': ' + e.message)
5}
6
1/*
2 throw new Error("error"); is a javascript line made up of two elements which
3 are used to gether at most of the times in making of javascript libraries,
4 for people to use to make an action easier than normal, to give errors when
5 the people gave a spelling error or syntax error, as they gave a string
6 instead of a number, etc.
7*/
8
9// Is't usage is:
10throw new Error("error");
11
12/*
13 Like I said, throw new Error("error") has two elements, the first element
14 is throw and the second element is new Error("error")
15
16 throw is used to throw something in the console box, here it is used to
17 throw an error
18
19 new Error("error") is used to create a new Error object which has the
20 argument as "error", the argument is the error which should be shown in the
21 console, Error is an object which is predefined in javascript using a
22 constructor, you cannot change its definition using another constructor
23 named as Error
24
25 Most of the times, they both are used in pairs directly, in some rare, they
26 are used in pairs indirectly i.e. storing the error object in a variable and
27 throwing it in the console using throw
28
29 This case might happen very rare when you need to call the error using two
30 different loal variables which are in different functions, then you may do
31 this:
32*/
33var error;
34
35function function1() {
36 let variable1 = "hello";
37 error = new Error("Error: " + variable1);
38 // error is now Error: hello
39}
40
41function function2() {
42 let varible2 = "world";
43 error = new Error(error + " " + variable2);
44 // and now the error is Error: hello world
45}
46