라이브러리
[PHP] Exception::getMessage - 예외 메시지를 가져옵니다.
PHP Exception 클래스
PHP의 Exception 클래스는 프로그램에서 발생하는 오류나 예외를 처리하는 데 사용됩니다. Exception 클래스는 Throwable 클래스를 상속합니다.
Exception::getMessage()
Exception::getMessage() 메소드는 Exception 객체가 생성될 때 발생한 오류 메시지를 반환합니다.
# 예제 1: Exception::getMessage() 사용하기
#hostingforum.kr
php
try {
$x = 5 / 0;
} catch (Exception $e) {
echo "오류 메시지: " . $e->getMessage() . "
";
}
위 예제에서, `$e->getMessage()`는 "Division by zero"를 출력합니다.
# 예제 2: 사용자 정의 Exception 클래스
#hostingforum.kr
php
class CustomException extends Exception {
public function __construct($message, $code = 0, Throwable $previous = null) {
parent::__construct($message, $code, $previous);
}
}
try {
throw new CustomException("사용자 정의 오류");
} catch (CustomException $e) {
echo "오류 메시지: " . $e->getMessage() . "
";
}
위 예제에서, `$e->getMessage()`는 "사용자 정의 오류"를 출력합니다.
Exception::getMessage() 메소드의 사용 사례
- 오류 메시지를 출력할 때 사용합니다.
- 오류 메시지를 로그 파일에 기록할 때 사용합니다.
- 오류 메시지를 사용자에게 전달할 때 사용합니다.
결론
PHP의 Exception::getMessage() 메소드는 Exception 객체가 생성될 때 발생한 오류 메시지를 반환합니다. 이 메소드는 오류 메시지를 출력, 로그 파일에 기록, 또는 사용자에게 전달할 때 사용할 수 있습니다.
참고자료
- [PHP Exception 클래스](https://www.php.net/manual/ko/class.exception.php)
- [PHP Exception::getMessage() 메소드](https://www.php.net/manual/ko/exception.getmessage.php)
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.