/    /  Javascript-Custom Errors

Javascript-Custom Errors

 

Custom errors in JavaScript are 8 types of error objects. which are raised in a try or catch expression depending on the error type. They are:

  • Error
  • EvalError
  • RangeError
  • ReferenceError
  • SyntaxError
  • TypeError
  • URIError
  • I analyzed them

 

Error:

 

This is a generic error. It’s the one all the other error objects inherit from.

 

It contains 2 properties:

 

message: Human-readable message that should explain what error happened.

name: Type of error occurred (the value of the particular error object name(TypeError / SyntaxError)).

 

EvalError:

 

It was used to specify that the global function eval() was used incorrectly and in a way incompatible with its definition.

 

RangeError:

 

A RangeError will fire and when a numeric value is not in its range of allowed values.

 

set an array length to a negative value:

 

[].length = -1 //RangeError: Invalid array length

 

set it to a number higher than 4294967295:

 

[].length = 4294967295 //4294967295
[].length = 4294967296 //RangeError: Invalid array length

 

most common range errors:

 

RangeError: Not a valid code point.

RangeError: invalid array length.

RangeError: invalid date.

RangeError: precision is out of range.

RangeError: radix must be an integer.

RangeError: repeat count must be less than infinity.

RangeError: repeat count must be non-negative.

 

ReferenceError:

 

It is indicated that an invalid reference value has been detected. A JavaScript programming language is trying to read a variable that does not exist.

 

dog //ReferenceError: dog is not defined
dog = 2 //ReferenceError: dog is not defined

 

most common reference errors:

 

ReferenceError: “x” is not defined.

ReferenceError: assignment to undeclared variable “x”.

ReferenceError: can’t access lexical declaration ‘X’ before initialization.

ReferenceError: deprecated caller or argument usage.

ReferenceError: invalid assignment left-hand side.

ReferenceError: reference to undefined property “x”.

 

SyntaxError:

 

It is raised when a syntax error is found in a program.

 

function statement without name:

 

function() {
 return 'Hi!'
}

 

//SyntaxError: function statement requires a name

 

comma after an object property:

 

const dog = {
 name: 'Roger'
 age: 5
}

 

//SyntaxError: missing } after property list

 

common syntax errors:

 

SyntaxError: “0”-prefixed octal literals and octal escape seq. are deprecated

SyntaxError: “use strict” not allowed in function(non-simple parameters).

SyntaxError: “x” is a reserved identifier.

SyntaxError: JSON.parse: bad parsing.

SyntaxError: Malformed formal parameter.

SyntaxError: Unexpected token.

SyntaxError:  //@ to indicate source URL pragmas is deprecated. Use //# instead.

SyntaxError: illegal character.

SyntaxError: invalid regular expression flag “x”.

SyntaxError: missing ) after argument list.

SyntaxError: missing ) after condition.

SyntaxError: missing : after property id.

SyntaxError: missing ; before statement.

SyntaxError: missing = in const declaration.

SyntaxError: missing \] after element list.

SyntaxError: missing formal parameter.

SyntaxError: missing name after . operator.

SyntaxError: missing variable name.

SyntaxError: missing } after function body.

SyntaxError: missing } after property list.

SyntaxError: redeclaration of formal parameter “x”.

SyntaxError: return not in function.

 

TypeError:

 

It happens when a value has a type that is different than the one expected.

 

trying to invoke a number:

 

1() //TypeError: 1 is not a function

 

common type errors:

 

TypeError: “x” has no properties

TypeError: “x” is (not) “y”

TypeError: “x” is not a constructor

TypeError: “x” is not a function

TypeError: “x” is not a non-null object

TypeError: “x” is read-only

TypeError: ‘x’ is not iterable

TypeError: More arguments needed

TypeError: invalid arguments

TypeError: cyclic object value

 

URIError:

 

It is raised when returning one of the global function() that work with URIs:

  • decodeURI()
  • decodeURIComponent()
  • encodeURI()
  • encodeURIComponent()

and passing an invalid URI.