php exception
PHP에서 예외(Exception)를 사용하여 오류를 처리하는 방법을 더 자세히 설명해 드리겠습니다. 예외는 실행 중에 발생한 오류 또는 예외적인 상황을 처리하는 데 사용됩니다.
1. 예외 클래스 정의:
PHP에서는 예외를 처리하기 위해 예외 클래스를 정의합니다. 다음은 간단한 예외 클래스의 예입니다:
```php
class CustomException extends Exception {
public function errorMessage() {
// 사용자 정의 예외 메시지 반환
return "오류 발생: " . $this->getMessage();
}
}```
2. 예외 던지기 (Throwing Exceptions):
예외를 발생시키려면 `throw` 키워드를 사용합니다. 다음은 예외를 발생시키는 예제입니다:
```php
function divide($numerator, $denominator) {
if ($denominator == 0) {
throw new CustomException("0으로 나눌 수 없습니다.");
}
return $numerator / $denominator;
}```
3. 예외 처리 (Exception Handling):
예외가 발생하면 `try...catch` 블록을 사용하여 처리할 수 있습니다.
```php
try {
$result = divide(10, 0);
echo "결과: " . $result;
} catch (CustomException $e) {
echo $e->errorMessage();
}```
위의 코드에서 `divide` 함수는 0으로 나눌 때 `CustomException`을 throw합니다. `try` 블록 내에서 예외가 발생하면 `catch` 블록에서 해당 예외를 잡아 처리합니다.
4. 예외 체인 (Exception Chaining):
PHP에서는 여러 예외를 연결하여 사용자 정의 예외를 더 풍부하게 만들 수 있습니다. 이를 통해 예외에 관련된 추가 정보를 포함할 수 있습니다.
```php
class AnotherCustomException extends Exception {
public function errorMessage() {
return "또 다른 오류 발생: " . $this->getMessage();
}
}
try {
try {
throw new CustomException("첫 번째 예외 발생");
} catch (CustomException $e) {
throw new AnotherCustomException("두 번째 예외 발생", 0, $e);
}
} catch (AnotherCustomException $e) {
echo $e->errorMessage();
}```
위의 코드에서 `AnotherCustomException`이 `CustomException`을 래핑하여 두 개의 예외가 연결되어 있습니다. 이러한 방식으로 예외를 더 상세하게 관리할 수 있습니다.
이것은 PHP에서 예외를 사용하여 오류를 처리하는 기본적인 방법입니다. 예외를 사용하면 코드를 더 견고하게 만들고 오류를 보다 효과적으로 관리할 수 있습니다.
| Number | Title | Author | Date | Votes | Views |
| 40 |
php formatter
siwon
|
2024.11.26
|
Votes -1
|
Views 1321
|
siwon | 2024.11.26 | -1 | 1321 |
| 39 |
html center 중앙정렬 tailwind
siwon
|
2024.07.27
|
Votes 0
|
Views 1640
|
siwon | 2024.07.27 | 0 | 1640 |
| 38 |
dropdown menu alpinejs 사용 버전
siwon
|
2024.04.30
|
Votes 0
|
Views 1565
|
siwon | 2024.04.30 | 0 | 1565 |
| 37 |
dropdown menu 간단 버전
siwon
|
2024.04.30
|
Votes 0
|
Views 1524
|
siwon | 2024.04.30 | 0 | 1524 |
| 36 |
The Standard PHP Library (SPL) is a collection of classes and interfaces that provide core functionality to PHP developers.
siwon
|
2023.10.24
|
Votes 0
|
Views 2202
|
siwon | 2023.10.24 | 0 | 2202 |
| 35 |
session 과 쿠키
siwon
|
2023.10.24
|
Votes 0
|
Views 1462
|
siwon | 2023.10.24 | 0 | 1462 |
| 34 |
Late Static Binding (LSB):메서드 내부에서 현재 클래스의 정적 메서드 또는 프로퍼티를 호출할 때 사용
siwon
|
2023.10.24
|
Votes 0
|
Views 1285
|
siwon | 2023.10.24 | 0 | 1285 |
| 33 |
PHP 예외 처리(Exception Handling)
siwon
|
2023.10.10
|
Votes 0
|
Views 1606
|
siwon | 2023.10.10 | 0 | 1606 |
| 32 |
php exception
siwon
|
2023.10.10
|
Votes 0
|
Views 1803
|
siwon | 2023.10.10 | 0 | 1803 |
| 31 |
예외(Exception)를 처리하기 위해 try...catch 블록을 사용하는 방법
siwon
|
2023.10.10
|
Votes 0
|
Views 1340
|
siwon | 2023.10.10 | 0 | 1340 |
| 30 |
Preserving Parent Class Functionality in overriding
siwon
|
2023.09.26
|
Votes 0
|
Views 1174
|
siwon | 2023.09.26 | 0 | 1174 |
| 29 |
oop 세부항목
siwon
|
2023.09.26
|
Votes 0
|
Views 1190
|
siwon | 2023.09.26 | 0 | 1190 |
| 28 |
method chaining
siwon
|
2023.09.25
|
Votes 0
|
Views 1420
|
siwon | 2023.09.25 | 0 | 1420 |
| 27 |
interface implements
siwon
|
2023.09.19
|
Votes 0
|
Views 1243
|
siwon | 2023.09.19 | 0 | 1243 |
|
siwon
|
2023.10.24
|
Votes 0
|
Views 1050
|
siwon | 2023.10.24 | 0 | 1050 | |
| 26 |
abstract class : 부모 class로 사용되며 자식(extends 한)에게 abstract method를 강제함(그들만의 방식으로)
siwon
|
2023.09.19
|
Votes 0
|
Views 1137
|
siwon | 2023.09.19 | 0 | 1137 |
| 25 |
isset() / unset()
siwon
|
2023.09.18
|
Votes 0
|
Views 1315
|
siwon | 2023.09.18 | 0 | 1315 |
| 24 |
magic methods-어떤 상황이 되면 call 하지 않아도 자동으로 실행되는 메소드
siwon
|
2023.09.18
|
Votes 0
|
Views 1321
|
siwon | 2023.09.18 | 0 | 1321 |
| 23 |
MD(markdown) file
siwon
|
2023.09.12
|
Votes 0
|
Views 1240
|
siwon | 2023.09.12 | 0 | 1240 |
| 22 |
usort
siwon
|
2023.08.30
|
Votes 0
|
Views 1278
|
siwon | 2023.08.30 | 0 | 1278 |
| 21 |
closure=unanimous function
siwon
|
2023.08.30
|
Votes 0
|
Views 1305
|
siwon | 2023.08.30 | 0 | 1305 |
|
siwon
|
2023.10.24
|
Votes 0
|
Views 3971
|
siwon | 2023.10.24 | 0 | 3971 | |
| 20 |
reference variable &
siwon
|
2023.08.29
|
Votes 0
|
Views 1297
|
siwon | 2023.08.29 | 0 | 1297 |
|
siwon
|
2023.08.30
|
Votes 0
|
Views 1227
|
siwon | 2023.08.30 | 0 | 1227 | |
| 19 |
PHP 변수 : 스칼라(Scalar), 복합(Composite), 그리고 리소스(Resource)
siwon
|
2023.08.22
|
Votes 0
|
Views 1376
|
siwon | 2023.08.22 | 0 | 1376 |
| 18 |
if : vs {}
siwon
|
2023.08.22
|
Votes 0
|
Views 1121
|
siwon | 2023.08.22 | 0 | 1121 |
| 17 |
null coalescing operator
siwon
|
2023.08.18
|
Votes 0
|
Views 1468
|
siwon | 2023.08.18 | 0 | 1468 |
| 16 |
arrary functions
siwon
|
2023.08.18
|
Votes 0
|
Views 1282
|
siwon | 2023.08.18 | 0 | 1282 |