--- id: php-exceptions title: "PHP Exceptions" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["try catch finally", "Exception object", "PHP μ˜ˆμ™Έ"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "exceptions", "try-catch"] raw_sources: ["https://www.w3schools.com/php/php_exceptions.asp"] applied_in: [] github_commit: "" --- # [[PHP Exceptions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) An uncaught exception doesn't just print an error β€” it terminates the ENTIRE script, including any code AFTER the throwing line (`echo 'Hello';` never runs in the uncaught example) β€” making exception handling not just about nice error messages but about whether the rest of the program executes at all. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Exception** β€” an unwanted/unexpected event during script execution; thrown by many built-in functions/classes. [S1] - **`throw`** β€” raises an exception. [S1] - **`try`** β€” wraps code that might throw. [S1] - **`catch(Exception $e)`** β€” handles a thrown exception; specifies the exception type and a variable name. [S1] - **`finally`** β€” runs regardless of whether an exception was caught. [S1] - **`new Exception(message, code, previous)`** β€” all three constructor parameters optional. [S1] - **Exception object methods** β€” `getMessage()`, `getPrevious()`, `getCode()`, `getFile()`, `getLine()`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Uncaught exception halts everything after it: `echo divide(5, 0); echo 'Hello';` β€” "Hello" never prints, since the fatal error terminates the script. [S1] - Basic try/catch: `try { echo divide(5, 0); } catch(Exception $e) { echo 'Error: ' . $e->getMessage(); }`. [S1] - try/catch/finally: adds `finally { echo '
Process complete.'; }` which runs no matter what. [S1] - Full exception info: `catch(Exception $e) { $file = $e->getFile(); $line = $e->getLine(); $code = $e->getCode(); $message = $e->getMessage(); echo "Exception thrown in $file on line $line: [Code $code] $message"; }`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **미처리 μ˜ˆμ™Έμ˜ 전체 쀑단 효과**: μ˜ˆμ™Έκ°€ μ²˜λ¦¬λ˜μ§€ μ•ŠμœΌλ©΄ λ°œμƒ 지점 μ΄ν›„μ˜ λͺ¨λ“  μ½”λ“œκ°€ μ‹€ν–‰λ˜μ§€ μ•Šκ³  슀크립트 μžμ²΄κ°€ μ’…λ£Œλœλ‹€λŠ” 점이 예제둜 직접 증λͺ…됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” divide() ν•¨μˆ˜μ˜ 0으둜 λ‚˜λˆ„κΈ° λ°©μ§€κ°€ μ˜ˆμ™Έ 처리의 ν‘œμ€€ ꡐ윑용 μ˜ˆμ‹œλ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Try/catch/finally with full exception info (PHP): ```php function divide($x, $y) { if ($y == 0) { throw new Exception("Cannot divide by zero."); } return $x / $y; } try { echo divide(5, 0); } catch (Exception $e) { echo "Exception in " . $e->getFile() . " on line " . $e->getLine() . ": " . $e->getMessage(); } finally { echo '
Process complete.'; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP JSON]], [[PHP Error Handling]], [[PHP Exception]] - **μ°Έμ‘° λ§₯락:** μ˜ˆμ™Έ 처리 기초(throw/try/catch/finally) β€” μ—λŸ¬ 처리(Error Handling) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP Exceptions β€” https://www.w3schools.com/php/php_exceptions.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP Exceptions" page (Astra wiki-curation, P-Reinforce v3.1 format).