Errors and Exceptions #30 Flashcards
What are the 7 error objects in JS ?
- Error
- EvalError
- RangeError
- ReferenceError
- SyntaxError
- TypeError
- URIError
What properties does Error contain?
- Message - returns the error description as a human readable message explaining what error occurred.
- Name- the type of error that occurred (TypeError or SyntaxError etc)
What does RangeError mean?
Fires when a numeric value is out of range. For example:
[ ].length = -1 //RangeError: Invalid array length
or
[ ].length = 4294967295 //4294967295
[ ].length = 4294967296 //RangeError: Invalid array length
What does ReferrenceError mean?
A ReferenceError indicates that an invalid reference value has been detected: a JavaScript program is trying to read a variable that does not exist. Example:
dog //ReferenceError: dog is not defined
dog = 2 //ReferenceError: dog is not defined
What does SyntaxError indicate?
Self explanatory, however see examples below:
A function statement without name:
function() { return 'Hi!' } //SyntaxError: function statement requires a name
Missing comma after an object property definition:
const dog = { name: 'Roger' age: 5 } //SyntaxError: missing } after property list
What does TypeError indicate?
A TypeError happens when a value has a type that’s different than the one expected.
The simplest example is trying to invoke a number:
1( ) //TypeError: 1 is not a function
What function does Finally perform?
To complete the try/catch statement JavaScript has another statement called finally, which contains code that is executed regardless of the program flow, if the exception was handled or not, if there was an exception or if there wasn’t:
try { //lines of code } catch (e) {
} finally {
}
You can use finally without a catch block, to serve as a way to clean up any resource you might have opened in the try block, like files or network requests:
try { //lines of code } finally {
}