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