--- id: cpp-exceptions title: "C++ Exceptions" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["try catch throw", "exception handling", "catch all (...)", "C++ μ˜ˆμ™Έ 처리"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "exceptions", "try-catch"] raw_sources: ["https://www.w3schools.com/cpp/cpp_exceptions.asp"] applied_in: [] github_commit: "" --- # [[CPP Exceptions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) C++ has the try/catch/throw mechanism that C's Error Handling chapter EXPLICITLY said C lacks entirely β€” meaning this is the single biggest error-handling divergence between the two languages: where C forced programmers into manual NULL-checking, `errno`, and `perror()` workarounds, C++ provides genuine LANGUAGE-LEVEL exception handling that can catch ANY thrown value, even without knowing its type in advance (via the `catch (...)` catch-all syntax). [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Exception** β€” C++'s technical term for the error that occurs when something goes wrong; "C++ will THROW an exception." [S1] - **`try`** β€” defines the code block to test for possible errors. [S1] - **`throw`** β€” triggers an exception, optionally carrying a VALUE (of any type, e.g. `throw 505;` or `throw age;`). [S1] - **`catch (type param)`** β€” handles the exception; its parameter TYPE must match what was thrown (e.g. `catch (int errorCode)` for an `int`-typed throw). [S1] - **`catch (...)`** β€” the "three dots" catch-all syntax; handles ANY exception type when you don't know (or don't need to distinguish) the thrown type in advance. [S1] - **No error = catch block skipped** β€” if the `try` block never throws, the `catch` block simply doesn't run. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic throw/catch with a custom error code: `try { throw 505; } catch (int errorCode) { cout << "Error occurred: " << errorCode; }`. [S1] - Real-life age-check example throwing the actual failing value: `try { int age = 15; if (age >= 18) { cout << "Access granted."; } else { throw (age); } } catch (int myNum) { cout << "Access denied. Age is: " << myNum; }`. [S1] - Catch-all syntax when the thrown type is unknown: `try { ... throw 505; } catch (...) { cout << "Access denied."; }` β€” handles the exception without needing to declare its type. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **Cμ—λŠ” μ—†λ˜ μ§„μ§œ μ˜ˆμ™Έ 처리 λ©”μ»€λ‹ˆμ¦˜**: C의 Error Handling 챕터가 "Cμ—λŠ” try/catchκ°€ μ—†λ‹€"κ³  λͺ…μ‹œν–ˆλ˜ 것과 μ •λ°˜λŒ€λ‘œ, C++λŠ” try/throw/catchλΌλŠ” μ–Έμ–΄ μ°¨μ›μ˜ μ˜ˆμ™Έ 처리λ₯Ό μ œκ³΅ν•˜λ©°, catch(...)둜 νƒ€μž…μ„ λͺ°λΌλ„ λͺ¨λ“  μ˜ˆμ™Έλ₯Ό μž‘μ„ 수 μžˆλ‹€λŠ” μ κΉŒμ§€ Cμ—λŠ” μ—†λ˜ κΈ°λŠ₯으둜 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) μ‚¬μš©μžμ˜ λ‚˜μ΄κ°€ 18μ„Έ 미만이면 μ˜ˆμ™Έλ₯Ό λ˜μ§€κ³  접근을 κ±°λΆ€ν•˜λŠ” age-check μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Catching any exception type without needing to know it in advance (C++): ```cpp try { int age = 15; if (age >= 18) { cout << "Access granted - you are old enough."; } else { throw 505; } } catch (...) { // catches any exception type cout << "Access denied - You must be at least 18 years old.\n"; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Errors]], [[CPP Input Validation]], [[C Error Handling]] - **μ°Έμ‘° λ§₯락:** μ˜ˆμ™Έ 처리 β€” μž…λ ₯ 검증(Input Validation) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Exceptions β€” https://www.w3schools.com/cpp/cpp_exceptions.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Exceptions" page (Astra wiki-curation, P-Reinforce v3.1 format).