JavaScript
HTML
CSS
jQuery
PHP
SQL
WordPress
JavaScript Reference

Quick Reference

The throw statement allows you to generate a custom error (throw an exception), which can be a String, a Number, a Boolean, or an Object.

				
					<!-- html element to place output -->
<p id="my_output"></p>
				
			

In the following, we use x as our value (let’s say it was input by the user in a form field). The user was asked to input a number between 1 and 5.

We then use the throw statement to look for errors (try) and output them (catch) to the html element.

If no errors are found, nothing will be output using the “catch”, and the code will continue to run after line 13.

				
					// variable
let x = 13;

// throw statement
try { 
	if(x == '')  throw 'You did not enter a value.';
    if(isNaN(x)) throw 'You did not enter a number.';
    if(x > 5)   throw 'The number entered is too high.';
    if(x < 1)  throw 'The number entered is too low.';
  }
catch(err) {
  	document.getElementById('my_output').innerHTML = err;
  }
				
			

Output

				
					The number entered is too high.
				
			

Syntax

				
					condition throw 'message';
				
			

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.

JavaScript
HTML
CSS
jQuery
PHP
SQL
WordPress

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

Feedback

If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.