[PHP]예외 처리시 메시지

김이홍·2024년 5월 9일

PHP

목록 보기
29/30

getMessage() 메서드는 예외 객체에 설정된 오류 메시지를 반환합니다. PHP에서 예외를 생성할 때 new Exception("메시지 내용")와 같은 형식으로 메시지를 지정할 수 있습니다.

try {
    $stmt = $conn->prepare("DELETE FROM users WHERE id=?");
    $stmt->bind_param("i", $id);
    if (!$stmt->execute()) {
        throw new Exception("삭제 실패: " . $stmt->error);
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

0개의 댓글