--- id: csharp-exceptions title: "C# Exceptions - Try..Catch" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["try catch finally throw C#", "e.Message", "C# μ˜ˆμ™Έ 처리"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.85 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["csharp", "programming-language", "w3schools", "exceptions"] raw_sources: ["https://www.w3schools.com/cs/cs_exceptions.php"] applied_in: [] github_commit: "" --- # [[CSharp Exceptions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The `try { } catch (Exception e) { } finally { }` structure is functionally identical to C++'s exception handling already documented in `[[CPP Exceptions]]`, but where C++ typically catches by REFERENCE to a specific exception type (`catch (std::exception& e)`) and reads its message via `.what()`, C# catches the generic base `Exception` type and reads the message via a `.Message` PROPERTY (no parentheses β€” consistent with the property-not-method pattern already confirmed for `.Length` in the Strings chapter); `throw new ArithmeticException("...")` also demonstrates that C# ships a family of ready-made, semantically-named exception classes (`ArithmeticException`, `FileNotFoundException`, `IndexOutOfRangeException`, `TimeOutException`) rather than requiring the programmer to define custom exception types from scratch for common cases. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Exception** β€” the technical term for an error C# "throws" when something goes wrong (bad input, coding errors, unforeseen conditions). [S1] - **`try { }`** β€” a block of code tested for errors during execution. [S1] - **`catch (Exception e) { }`** β€” a block that runs IF an error occurs inside the paired `try` block; `e` is a variable holding the exception object. [S1] - **`e.Message`** β€” a property (no parentheses) on the caught exception, describing what went wrong. [S1] - **`finally { }`** β€” runs AFTER the try/catch, regardless of whether an error occurred or was caught. [S1] - **`throw new ExceptionType("message")`** β€” creates and raises a CUSTOM error, using one of C#'s built-in exception classes (`ArithmeticException`, `FileNotFoundException`, `IndexOutOfRangeException`, `TimeOutException`, etc.). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Unhandled error: `int[] myNumbers = {1, 2, 3}; Console.WriteLine(myNumbers[10]);` β†’ `System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'`. [S1] - Caught with built-in message: `try { ...myNumbers[10]... } catch (Exception e) { Console.WriteLine(e.Message); }` β†’ `"Index was outside the bounds of the array."`. [S1] - Caught with custom message: `catch (Exception e) { Console.WriteLine("Something went wrong."); }` β†’ `"Something went wrong."` (note: `e` is available but not necessarily used). [S1] - With `finally`: adding `finally { Console.WriteLine("The 'try catch' is finished."); }` runs that line AFTER the catch block regardless of outcome. [S1] - Custom throw: `static void checkAge(int age) { if (age < 18) { throw new ArithmeticException("Access denied - You must be at least 18 years old."); } else { Console.WriteLine("Access granted - You are old enough!"); } } ... checkAge(15);` β†’ throws `System.ArithmeticException: 'Access denied...'`; `checkAge(20);` β†’ prints `"Access granted - You are old enough!"`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **C++의 μ˜ˆμ™Έ μ²˜λ¦¬μ™€ κ΅¬μ‘°λŠ” 동일, λ©”μ‹œμ§€ μ ‘κ·Ό 방식이 닀름**: try/catch/finally ꡬ쑰 μžμ²΄λŠ” `[[CPP Exceptions]]`μ—μ„œ ν™•μΈλœ C++와 차이가 μ—†μ§€λ§Œ, C++κ°€ 보톡 `.what()` λ©”μ„œλ“œλ‘œ μ˜ˆμ™Έ λ©”μ‹œμ§€λ₯Ό μ–»λŠ” 반면 C#은 `.Message`λΌλŠ” ν”„λ‘œνΌν‹°(κ΄„ν˜Έ μ—†μŒ)둜 μ ‘κ·Όν•œλ‹€λŠ” 점이 확인됨 β€” Strings μ±•ν„°μ—μ„œ ν™•μΈλœ ν”„λ‘œνΌν‹° vs λ©”μ„œλ“œ ꡬ뢄이 μ˜ˆμ™Έ 객체에도 κ·ΈλŒ€λ‘œ 적용됨. [S1] - **μ¦‰μ‹œ μ‚¬μš© κ°€λŠ₯ν•œ λ‹€μ–‘ν•œ λ‚΄μž₯ μ˜ˆμ™Έ 클래슀 제곡**: `ArithmeticException`, `FileNotFoundException`, `IndexOutOfRangeException`, `TimeOutException` λ“± μ˜λ―Έκ°€ λͺ…ν™•ν•œ μ˜ˆμ™Έ ν΄λž˜μŠ€λ“€μ΄ κΈ°λ³Έ μ œκ³΅λ˜μ–΄, throwν•  λ•Œ 맀번 μ»€μŠ€ν…€ μ˜ˆμ™Έ νƒ€μž…μ„ μƒˆλ‘œ μ •μ˜ν•  ν•„μš”κ°€ μ—†λ‹€λŠ” 점이 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) λ‚˜μ΄κ°€ 18μ„Έ 미만이면 ArithmeticException을 throwν•˜κ³ , 18μ„Έ 이상이면 "Access granted"λ₯Ό 좜λ ₯ν•˜λŠ” checkAge() ν•¨μˆ˜ μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) try/catch/finally with a custom throw (C#): ```csharp static void checkAge(int age) { if (age < 18) { throw new ArithmeticException("Access denied - You must be at least 18 years old."); } else { Console.WriteLine("Access granted - You are old enough!"); } } try { checkAge(15); } catch (Exception e) { Console.WriteLine(e.Message); // property, no parentheses } finally { Console.WriteLine("The 'try catch' is finished."); } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.85 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C# Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CSharp User Input]], [[CSharp Files]], [[CPP Exceptions]], [[CSharp Strings]] - **μ°Έμ‘° λ§₯락:** Exceptions μ„Ήμ…˜μ˜ 유일 챕터 β€” Files μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C# Exceptions - Try..Catch β€” https://www.w3schools.com/cs/cs_exceptions.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# Exceptions - Try..Catch" page (Astra wiki-curation, P-Reinforce v3.1 format).