--- 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).