9609c04755
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더). - Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외) - Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체) - Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속) 각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
162 lines
6.4 KiB
Markdown
162 lines
6.4 KiB
Markdown
---
|
|
id: javascript-error-statements
|
|
title: "JavaScript Error Statements"
|
|
category: "Frontend"
|
|
status: "draft"
|
|
verification_status: "conceptual"
|
|
canonical_id: ""
|
|
aliases: ["try catch finally", "throw statement", "error statements", "input validation", "exception handling"]
|
|
duplicate_of: ""
|
|
source_trust_level: "B"
|
|
confidence_score: 0.88
|
|
created_at: 2026-06-23
|
|
updated_at: 2026-06-23
|
|
review_reason: ""
|
|
merge_history: []
|
|
tags: ["javascript", "js", "web", "frontend", "w3schools", "errors", "try-catch", "throw"]
|
|
raw_sources: ["https://www.w3schools.com/js/js_errors.asp"]
|
|
applied_in: []
|
|
github_commit: ""
|
|
---
|
|
|
|
# [[JavaScript Error Statements]]
|
|
|
|
## 🎯 한 줄 통찰 (One-line insight)
|
|
The `try`, `catch`, `finally`, and `throw` statements form JavaScript's error-handling block: try risky code, catch failures, optionally always run cleanup in `finally`, and raise custom errors with `throw`. [S1]
|
|
|
|
## 🧠 핵심 개념 (Core concepts)
|
|
- **The try statement** handles errors during execution without stopping the program; it works with `catch`, and optionally `finally` and `throw`. [S1]
|
|
- **The try block** holds code that might throw an error; the catch block is skipped if no error occurs. [S1]
|
|
- **The catch block** executes only if an error occurs in the try block, providing error details through an error object. [S1]
|
|
- **The finally block (optional)** runs after try and catch regardless of outcome, typically for cleanup. [S1]
|
|
- **JavaScript throws errors** by creating an Error object with `name` and `message` properties. [S1]
|
|
- **The throw statement** lets you create custom errors; you can throw strings, numbers, booleans, or objects. [S1]
|
|
|
|
## 🧩 추출된 패턴 (Extracted patterns)
|
|
- **Validate-then-throw** — check input conditions and `throw` a descriptive value for each failure, then format a message in `catch`. [S1]
|
|
- **Always clean up in `finally`** — use `finally` to reset state (e.g. clear an input field) whether or not an error occurred. [S1]
|
|
- **JS + HTML validation combo** — modern browsers often combine JavaScript with built-in HTML validation rules defined in HTML attributes (e.g. `min`, `max`, `step`). [S1]
|
|
|
|
## 📖 세부 내용 (Details)
|
|
**The Try Statement**
|
|
The `try` statement handles errors during code execution without stopping the program. It works with `catch`, and optionally with `finally` and `throw`. [S1]
|
|
|
|
**The Try Block / The Catch Block**
|
|
The try block contains code that might throw an error; the catch block is skipped if no error occurs. The catch block executes only if an error occurs, providing error details through an error object: [S1]
|
|
```javascript
|
|
try {
|
|
// Code that may cause an error
|
|
} catch (error) {
|
|
// Code to handle the error
|
|
}
|
|
```
|
|
|
|
**The Finally Block (Optional)**
|
|
The finally block executes after try and catch regardless of outcome, typically for cleanup tasks: [S1]
|
|
```javascript
|
|
try {
|
|
// Block of code to try
|
|
} catch(err) {
|
|
// Block of code to handle errors
|
|
} finally {
|
|
// Block of code to be executed regardless of the try / catch result
|
|
}
|
|
```
|
|
|
|
**JavaScript Throws Errors**
|
|
When errors occur, JavaScript creates an Error object with `name` and `message` properties. [S1]
|
|
|
|
**The Throw Statement**
|
|
The `throw` statement allows creation of custom errors and can throw strings, numbers, booleans, or objects. [S1]
|
|
|
|
**Input Validation Example**
|
|
Validates a number between 5 and 10, throwing `"empty"`, `"not a number"`, `"too low"`, or `"too high"` as appropriate: [S1]
|
|
```javascript
|
|
function myFunction() {
|
|
const message = document.getElementById("p01");
|
|
message.innerHTML = "";
|
|
let x = document.getElementById("demo").value;
|
|
try {
|
|
if(x.trim() == "") throw "empty";
|
|
if(isNaN(x)) throw "not a number";
|
|
x = Number(x);
|
|
if(x < 5) throw "too low";
|
|
if(x > 10) throw "too high";
|
|
} catch(err) {
|
|
message.innerHTML = "Input is " + err;
|
|
}
|
|
}
|
|
```
|
|
|
|
**HTML Validation**
|
|
Modern browsers will often use a combination of JavaScript and built-in HTML validation, using predefined validation rules defined in HTML attributes. [S1]
|
|
|
|
**Finally Example**
|
|
A function that validates input and uses `finally` to clear the input field regardless of validation outcome: [S1]
|
|
```javascript
|
|
function myFunction() {
|
|
const message = document.getElementById("p01");
|
|
message.innerHTML = "";
|
|
let x = document.getElementById("demo").value;
|
|
try {
|
|
if(x.trim() == "") throw "is empty";
|
|
if(isNaN(x)) throw "is not a number";
|
|
x = Number(x);
|
|
if(x > 10) throw "is too high";
|
|
if(x < 5) throw "is too low";
|
|
} catch(err) {
|
|
message.innerHTML = "Error: " + err + ".";
|
|
} finally {
|
|
document.getElementById("demo").value = "";
|
|
}
|
|
}
|
|
```
|
|
|
|
## 🛠️ 적용 사례 (Applied in summary)
|
|
The two `myFunction` examples are the canonical applied cases — a numeric input validator that throws descriptive strings on bad input, and a variant that additionally clears the field in `finally`. No external project/commit applications found in the source.
|
|
|
|
## 💻 코드 패턴 (Code patterns)
|
|
Validate input by throwing descriptive values, then format in `catch` (language: JavaScript):
|
|
```javascript
|
|
try {
|
|
if(x.trim() == "") throw "empty";
|
|
if(isNaN(x)) throw "not a number";
|
|
x = Number(x);
|
|
if(x < 5) throw "too low";
|
|
if(x > 10) throw "too high";
|
|
} catch(err) {
|
|
message.innerHTML = "Input is " + err;
|
|
}
|
|
```
|
|
Guarantee cleanup with `finally`:
|
|
```javascript
|
|
try {
|
|
// risky work
|
|
} catch(err) {
|
|
// handle
|
|
} finally {
|
|
document.getElementById("demo").value = "";
|
|
}
|
|
```
|
|
|
|
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
|
No contradictions found in the source.
|
|
|
|
## ✅ 검증 상태 및 신뢰도
|
|
- **상태:** draft
|
|
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
|
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
|
- **신뢰 점수:** 0.88
|
|
- **중복 검사 결과:** 신규 생성 (New discovery)
|
|
|
|
## 🔗 지식 그래프 (Knowledge Graph)
|
|
- **상위/루트:** [[JavaScript Tutorial]]
|
|
- **관련 개념:** [[JavaScript Errors Intro]], [[JavaScript Silent Errors]], [[JavaScript Error Object]], [[JavaScript Debugging]]
|
|
- **참조 맥락:** The mechanics page for the four error keywords (try/catch/finally/throw) used to build robust input validation.
|
|
|
|
## 📚 출처 (Sources)
|
|
- [S1] W3Schools — JavaScript Error Statements — https://www.w3schools.com/js/js_errors.asp
|
|
|
|
## 📝 변경 이력 (Change history)
|
|
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Error Statements" page (Astra wiki-curation, P-Reinforce v3.1 format).
|