Start · Sprachen · PHP · Referenz · try-catch

try-catch

Anweisung

Fängt Exceptions ab. <code>finally</code> wird immer ausgeführt — auch bei re-throw.

seit PHP 5.0.0 Kategorie: error

Signatur

try { … } catch (Throwable $e) { … } finally { … }

Beschreibung

Seit PHP 8.0 ist catch ohne Variable erlaubt: catch (SomeException). Mehrere Typen mit Union: catch (A|B $e).

Rückgabewert

Typ
void

Beispiele

Multi-Type

<?php

try {
    riskyCall();
} catch (NetworkException | TimeoutException $e) {
    logger()->warning($e->getMessage());
} finally {
    cleanup();
}