JavaScript Errors
Throw, and Try...Catch...Finally
The try
statement sets up a section of code to be executed and tested for any potential errors or issues.
The catch
statement sets up a block of code to manage errors that may occur.
The finally
statement sets up a block of code to execute, no matter what happens in the preceding code.
The throw
statement defines a custom error.
Errors Will Happen!
When running JavaScript code, various errors may occur.
Errors in programming can happen when a programmer makes mistakes in their code, when the input provided is incorrect, or due to unexpected events.
JavaScript recognizes an adddlert as a mistake and runs the catch code to deal with it.
JavaScript try and catch
The try
statement lets you specify a section of code that will be checked for errors as it runs.
The catch
statement allows you to define a block of code to
be executed, if an error occurs in the try block.
The JavaScript commands try
and catch
work together in pairs:
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
JavaScript Throws Errors
When something goes wrong in JavaScript, it usually stops and shows an error message.
The technical term for this is: JavaScript will encounter a problem (an error) and show an exception.
JavaScript will actually create an Error object with two properties: name and message.
The throw Statement
The throw
statement lets you make your own error in the code.
You can technically cause an error by throwing an exception.
The problem can be a JavaScript string, number, boolean, or an object.
throw "Too big"; // throw a text
throw 500; // throw a number
When you combine throw
with try
and catch
, you gain the ability to manage how your program behaves and create personalized error messages.
Input Validation Example
This sample looks at input. If the value is incorrect, it triggers an exception (err).
The catch statement catches the exception (err) and shows a personalized error message.
HTML Validation
The code above is just an example.
Contemporary web browsers typically rely on both JavaScript and built-in HTML validation. They utilize preset validation rules specified in HTML attributes to validate input.
<input id="demo" type="number" min="5" max="10" step="1">
You can learn more about checking if forms are filled out correctly in a later part of this guide.
The finally Statement
The finally
statement allows you to run code after the try and catch blocks, no matter what the outcome is.
Syntax
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
finally {
Block of code to be executed regardless of the try / catch result
}
The Error Object
JavaScript includes a built-in error object that gives details about errors when they happen.
The error object has two helpful properties: name and message.
Error Object Properties
Property | Description |
---|---|
name | Sets or returns an error name |
message | Sets or returns an error message (a string) |
Error Name Values
Six different values can be returned by the error name property:
Error Name | Description |
---|---|
EvalError | An error has occurred in the eval() function |
RangeError | A number "out of range" has occurred |
ReferenceError | An illegal reference has occurred |
SyntaxError | A syntax error has occurred |
TypeError | A type error has occurred |
URIError | An error in encodeURI() has occurred |
The six different values are described below.
Eval Error
A problem in the eval() function is shown by an EvalError
.
Recent JavaScript updates don't throw EvalError anymore. Use SyntaxError instead.
Range Error
If you use a number that's not allowed, a RangeError
is triggered.
For instance: You can't make a number have 500 significant digits.
Reference Error
An error called ReferenceError
occurs when you try to use a variable that hasn't been declared.
Syntax Error
If there's a mistake in your code, a SyntaxError
will be generated when you attempt to run it.
Type Error
An error called TypeError
occurs when you use a value that doesn't match the expected types.
URI (Uniform Resource Identifier) Error
The URIError
occurs when you use forbidden characters in a URI function.
Non-Standard Error Object Properties
Mozilla and Microsoft have their own special ways of describing certain error properties.
fileName (Mozilla)
lineNumber (Mozilla)
columnNumber (Mozilla)
stack (Mozilla)
description (Microsoft)
number (Microsoft)
Avoid employing these attributes on websites accessible to the public. They will not work in all browsers.