1

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?
2
  • 4
    There should be no die at all. If you let the script die at this point, then there will be no catching of anything later - dead is dead. Commented Sep 25, 2020 at 10:08
  • Not sure if it is a dupe of stackoverflow.com/questions/15318368/… (although that is myqli not pdo - but the principle is the same). Commented Sep 25, 2020 at 10:18

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

When I use this in PHPStorm, I get this notice Undefined class 'stdClass'