|
12 | 12 | | [The `location` and `history` Objects](#the-location-and-history-objects) | |
13 | 13 | | [The `navigator` Object](#the-navigator-object) | |
14 | 14 | | [Working with Dates](#working-with-dates) | |
| 15 | +| [The Error Object](#the-error-object) | |
15 | 16 |
|
16 | 17 | Below topics will be covered and for its practical application, [Planner Project](project_planner/) will be used as reference where concept of classes and functions are demonstrated: |
17 | 18 |
|
@@ -396,3 +397,53 @@ const currentSecond = currentDate.getSeconds(); |
396 | 397 | ``` |
397 | 398 |
|
398 | 399 | Note that the `Date()` function uses the local time zone of the user's computer. If you need to work with a specific time zone or date format, you may need to use a library like `moment.js` or perform manual calculations. |
| 400 | + |
| 401 | +## The [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) Object |
| 402 | + |
| 403 | +In JavaScript, the `Error` object is a built-in object that represents an error that occurs during the execution of a program. When an error occurs, an `Error` object is created and thrown, which can be caught and handled by the program. |
| 404 | + |
| 405 | +The `Error` object has several properties and methods that provide information about the error: |
| 406 | + |
| 407 | +- [`name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name): A string that specifies the name of the error. |
| 408 | +- [`message`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message): A string that specifies a human-readable description of the error. |
| 409 | +- [`stack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack): A string that specifies a stack trace of the error. |
| 410 | +- [`toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString): A method that returns a string representation of the error. |
| 411 | + |
| 412 | +Here's an example of how to create and throw an `Error` object: |
| 413 | + |
| 414 | +```javascript |
| 415 | +function divide(a, b) { |
| 416 | + if (b === 0) { |
| 417 | + throw new Error('Cannot divide by zero'); |
| 418 | + } |
| 419 | + return a / b; |
| 420 | +} |
| 421 | + |
| 422 | +try { |
| 423 | + const result = divide(10, 0); |
| 424 | + console.log(result); |
| 425 | +} catch (e) { |
| 426 | + console.error(e.name + ': ' + e.message); |
| 427 | +} |
| 428 | +``` |
| 429 | + |
| 430 | +In this example, the `divide()` function checks if the second argument is zero and throws an `Error` object if it is. The program then catches the error using a `try...catch` statement and logs the error message to the console. |
| 431 | + |
| 432 | +You can also create custom `Error` objects by extending the `Error` class: |
| 433 | + |
| 434 | +```javascript |
| 435 | +class CustomError extends Error { |
| 436 | + constructor(message) { |
| 437 | + super(message); |
| 438 | + this.name = 'CustomError'; |
| 439 | + } |
| 440 | +} |
| 441 | + |
| 442 | +try { |
| 443 | + throw new CustomError('Something went wrong'); |
| 444 | +} catch (e) { |
| 445 | + console.error(e.name + ': ' + e.message); |
| 446 | +} |
| 447 | +``` |
| 448 | + |
| 449 | +In this example, a custom `Error` object is created by extending the `Error` class and adding a custom name. The error is then thrown and caught using a `try...catch` statement, just like before. |
0 commit comments