JavaScript throw statement

The javaScript throw statement can be used to intentionally throw a user-defined exception or error if you prefer. It can be used as a way to stop execution of a javaScript program in the event that some kind of essential condition is not in order, or it can be used with try catch statements and other means of error handing with custom events rather that just what happens out of the box with javaScript.

In some cases I might use it as a way to intentionally throw a wrench into a machine sort of speak to stop execution of a program at a certain point as a means of debugging, but as of late I prefer to use alternatives to that to catch a state of affairs as to what is happening at a certain point in time. So for the most part the throw statement is just used in the process of making custom errors.

So lets take a look at some example of the javaScript throw keyword in action.

1 - javaScript throw basics

To use a throw statement is a basic way just type the throw keyword followed by a value that reflects what the error is about. For example I could have an add numbers method that will throw and error if I pass it a value other than a Number.

1
2
3
4
5
6
7
8
9
var addNumbers = function (a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw 'must give numbers';
}
return a + b;
};
console.log( addNumbers(15,5) ); // 20
addNumbers('foo', []); // Error must give numbers

The value can be a string, number, boolean, or an object that should be given certain standard key value pairs more on that later. For now you should get the basic idea at least, the throw statement will cause an error to happen, and then an additional value can be passed as a way to shed some light on what that error is.

2 - javaScript throw defined with an object

So a string can be used to describe the user defined Error, but it might be best to use an Object or The Error constructor to create an object with message and name properties that help to better identify what is wrong when the Error is thrown. This standatd object of sorts can be called you guessed it an error object that contains properties like message that is used to set the string message of the error, but also properties like name, line number, and so forth

1
2
3
4
5
throw {
message: 'this error is defined with an object',
name: 'ObjectDefinedError'
}
// ObjectDefinedError: this error is defined with an object

So now that we know how to create errors with the javaScript throw statement, maybe now is the time to cover a thing or two about some basic error handling.

3 - javaScript throw and try catch blocks

When an Error is thrown any catch statement present will of course be executed which can be used to handle the Error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var process = function (str) {
if (str === 'bar') {
console.log('foobar');
}
throw {
message: 'must give bar',
name: 'NoBarError'
};
};
try {
process('foo');
} catch (e) {
console.log(e.message); // 'must give bar'
}

4 - Conclusion

So the javaScript throw statement is what is used in javaScript to throw a custom user define error. However it is just one thing that comes to mind when it comes to creating and working with user defined errors. There is not just creating error objects after all, but also how to go about handling them. The javaScript try and catch blocks can be used as a way to define some code that will run in the event that a user define error happens.