<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
//basic
try{
const array = new Array(999999999999999999)
} catch (exception) {
console.log(exception)
console.log()
console.log(`exception name : ${exception.name}`)
console.log(`exception essage : ${exception.message}`)
}
//Forcibly raising and catching exceptions
function divide(a,b){
if (b===0){
throw 'do not divide 0'
}
return a/b
}
console.log(divide(10,2))
console.log(divide(10,0))
//Forcibly raising exceptions
function test(object){
console.log(object.a + object.b)
}
test({})
function test1(object){
if(object.a !== undefined && object.b !== undefined){
console.log(object.a + object.b)
} else {
throw new Error ("do not point at a,b")
}
}
test1({})
</script>
</head>
<body>
</body>
</html>
### Basic `try...catch`
- Attempts to create an array with an extremely large size, which exceeds the maximum array size limit and throws a `RangeError`.
- The `catch` block catches this exception, logs the entire exception object to the console, and then logs specific properties: the exception's name (`exception.name`) and message (`exception.message`).
### Forcibly Raising and Catching Exceptions
- The `divide(a, b)` function demonstrates manually throwing an exception using the `throw` statement when attempting to divide by zero.
- The example calls `divide(10, 2)` successfully, but `divide(10, 0)` attempts to perform division by zero, which triggers the `throw` statement. However, there is a missing `try...catch` block around the `divide(10, 0)` call, resulting in an uncaught exception that stops the execution of subsequent scripts.
### Forcibly Raising Exceptions in Object Property Access
- The `test(object)` function attempts to add two properties of an object without checking if they exist. Passing an empty object (`{}`) leads to a `TypeError` due to attempting to access undefined properties.
- `test1(object)` improves on this by checking if both properties `a` and `b` exist before attempting to add them. If either is undefined, it throws an `Error` with a custom message. However, similar to the `divide` function example, the call to `test1({})` is not wrapped in a `try...catch` block, leading to an uncaught exception.
### Issues and Recommendations
- **Uncaught Exceptions**: The document does not include `try...catch` blocks around the calls to `divide(10, 0)` and `test1({})`, which results in uncaught exceptions. To gracefully handle these errors, wrap these calls in `try...catch` blocks.
```javascript
try {
console.log(divide(10, 0));
} catch (error) {
console.log(error);
}
try {
test1({});
} catch (error) {
console.log(error.message);
}
```
- **Stopping Execution**: Uncaught exceptions will stop the execution of the script. Proper error handling ensures that the application can recover from errors or at least fail gracefully.
- **Security and Performance**: While catching exceptions is critical for robust applications, relying solely on `try...catch` for control flow is not recommended. Instead, perform upfront validation where possible, and use exceptions for truly exceptional, unexpected conditions.
### Conclusion
The examples highlight the importance of anticipating potential errors in your code and handling them appropriately to maintain application stability and provide meaningful feedback to developers or users about what went wrong.
'개념 > 혼자 공부하는 Javascript' 카테고리의 다른 글
chapter 9) combination get keyword and set keyword (0) | 2024.12.27 |
---|---|
chapter 8) Except (0) | 2024.12.26 |
chapter 8) try catch finally (0) | 2024.12.24 |
chapter 7) Creating a document object (0) | 2024.12.23 |
chapter 7) DomContentLoaded event (0) | 2024.12.22 |