PHP 캐치 키워드
예시
예외를 잡아라:
<?php
try {
throw new Exception("This is an exception");
}
catch(Exception $e) {
echo $e->getMessage();
}
?>
정의 및 사용
키워드는 이전 try 블록catch
의 코드에서 throw된 예외를 처리하는 데 사용 됩니다 .
관련 페이지
throw
키워드 .
try
키워드 .
finally
키워드 .
PHP 예외 튜토리얼 에서 try..catch.finally(예외)에 대해 자세히 알아보세요 .
더 많은 예
예시
여러 유형의 예외에 catch를 사용합니다.
<?php
try {
$rand = rand(0, 2);
switch($rand) {
case 0: throw new Exception();
case 1: throw new
OutOfBoundsException();
case 2: throw new
LogicException();
}
} catch(OutOfBoundsException $e) {
echo "Caught an out of bounds exception";
} catch(LogicException $e) {
echo "Caught a logic exception";
} catch(Exception $e) {
echo
"Caught an ordinary exception";
}
?>
❮ PHP 키워드