user13553026
user13553026

Reputation:

PHP Try Catch logical correct

I have this try and catch:

try {
    $stmt = $this->pdo->prepare("Select ...");
    $stmt->execute() or die(Log::error("DB-Error (Reservation): ", ['error' => $stmt->errorInfo()]));
    return $stmt->fetchAll(PDO::FETCH_OBJ);
} catch (PDOException $e) {
    Log::error("DB-Error (Reservation): ", ['error' => $e->getMessage(), 'errorcode' => (int)$e->getCode()]);
    return false;
} catch (\Exception $e) {
    Log::error("Error (Reservation): ", ['error' => $e->getMessage()]);
    return false;
}

I have 1 questions:

  1. Is this one needed: $stmt->execute() or die(Log::error("DB-Error (Reservation): ", ['error' => $stmt->errorInfo()])); or can I simply use $stmt->execute() or die(); because I catch the error later?

Upvotes: 1

Views: 48

Answers (1)

Dmitry Leiko
Dmitry Leiko

Reputation: 4372

There is should be no die you can try code below:

try {
    $stmt = $this->pdo->prepare("Select ...");
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_OBJ);
} catch (PDOException $e) {
    Log::error("DB-Error (Reservation): ", ['error' => $e->getMessage(), 'errorcode' => (int)$e->getCode()]);
    return new stdClass();
} catch (\Exception $e) {
    Log::error("Error (Reservation): ", ['error' => $e->getMessage()]);
    return new stdClass();
}

Upvotes: 1

Related Questions