Start · Sprachen · PHP · Referenz · throw

throw

Anweisung

Wirft eine Exception. Seit PHP 8.0 auch als Ausdruck in ternären Operatoren oder <code>match</code>.

seit PHP 5.0.0 Kategorie: error

Signatur

throw new Exception(…);

Beschreibung

Die Kontrolle springt zum nächsten passenden catch-Block.

Rückgabewert

Typ
never

Beispiele

Klassisch

<?php

if (!$user) {
    throw new NotFoundException("User $id nicht da");
}

Als Ausdruck (PHP 8+)

<?php

$user = User::find($id) ?? throw new NotFoundException("User $id");