- Syntax error - The error occurs when you use a predefined syntax incorrectly.
In the above example, an opening bracket is missing in the code, which invokes the Syntax error constructor.
- Reference Error - In a case where a variable reference can't be found or hasn't been declared, then a Reference error occurs.
- Type Error - An error occurs when a value is used outside the scope of its data type.
- RangeError - There is an error when a range of expected values is required, as shown below:
- URI Error - When the wrong character(s) are used in a URI function, the error is called.
- Internal Error - In the JS engine, this error occurs most often when there is too much data and the stack exceeds its critical size. When there are too many recursion patterns, switch cases, etc., the JS engine gets overwhelmed.
Output: Its output will be like InternalError.
Errors in programming can be divided into two types. These are:
- Program Error: - In this case, the program might need to handle this error through its error handlers. An example could be network disconnection, timeout error, etc.
- Developer Error: - The programmer has caused an error. It can be a syntax error, a logical error, a semantic error, etc.
The try...catch...finally Statement
After the try block, there must either be a catch block or a finally block (or both). The catch block is executed if an exception occurs in the try block. After try/catch, finally is executed unconditionally. Let's see an example:
Output: The below statements will be shown in an alert box.
The throw statement can be used to raise built-in exceptions or your customized ones.
Output: The below statement will be shown in an alert box.
The onerror() Method
There are three pieces of information provided by the onerror event handler that identifies the error's exact nature.
Error message − A message similar to the one displayed by the browser when an error occurs. URL − The file where the error occurred.
Line number− This is the line number in the URL where the error occurred.
- An error is a statement that interferes with the proper operation of the program.
- The article allows you to easily identify the source of an error, whether it occurs in your terminal or browser.