yoklainterview sim

Backend Php Exceptions Interview Questions

75 verified Backend Php Exceptions interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Php ExceptionsDifficulty 1
try {
    throw new Exception("boom");
} catch (Exception $e) {
    echo "caught: " . $e->getMessage();
}

What is printed?
  • acaught: boom
  • bboom
  • cFatal error: uncaught exception
  • dNothing is printed
Explanation:The thrown Exception has message "boom". The catch block matches Exception and echoes the literal prefix plus getMessage(), producing "caught: boom".
Php ExceptionsDifficulty 1
function run() {
    try {
        echo "A";
    } finally {
        echo "B";
    }
}
run();

What is printed?
  • aA
  • bB
  • cAB
  • dBA
Explanation:No exception is thrown, so the try block runs normally and prints "A". The finally block always executes afterward, printing "B". Output is "AB".
Php ExceptionsDifficulty 2
In PHP 8, calling a function declared with strict_types=1 using an argument of the wrong type throws a TypeError. Does catch (Exception $e) catch a TypeError?
  • aYes, because catch (Exception $e) is defined to match every internal throwable class PHP ships with
  • bNo — TypeError extends Error, not Exception, so a catch (Exception $e) block will not catch it
  • cOnly in PHP versions before 8.0, since the Error/Exception split did not exist yet back then
  • dOnly if strict_types is disabled somewhere earlier in the same request
Explanation:Error and Exception are separate class hierarchies that both implement Throwable, but neither extends the other. TypeError extends Error, so it will not be caught by catch (Exception $e); you would need catch (Error $e) or catch (Throwable $e).
Php ExceptionsDifficulty 2
function parse(int $mode) {
    try {
        if ($mode === 1) throw new InvalidArgumentException("bad arg");
        if ($mode === 2) throw new RuntimeException("bad state");
        return "ok";
    } catch (InvalidArgumentException|RuntimeException $e) {
        return "handled: " . $e->getMessage();
    }
}
echo parse(2);

What is printed?
  • aok
  • bhandled: bad arg
  • cFatal error: invalid multi-catch syntax
  • dhandled: bad state
Explanation:catch (InvalidArgumentException|RuntimeException $e) is valid multi-catch syntax (available since PHP 7.1) matching either type. With $mode = 2, a RuntimeException("bad state") is thrown and caught, producing "handled: bad state".
Php ExceptionsDifficulty 1
class NotFoundException extends Exception {}

try {
    throw new NotFoundException("user missing");
} catch (Exception $e) {
    echo get_class($e) . ": " . $e->getMessage();
}

What is printed?
  • aNotFoundException: user missing
  • bException: user missing
  • cNotFoundException: missing
  • dNothing is printed, because get_class() cannot be called on a caught exception object
Explanation:class NotFoundException extends Exception {} is already a fully valid custom exception with no extra code needed. catch (Exception $e) matches it because NotFoundException is-a Exception. get_class($e) returns the object's actual runtime class — NotFoundException — not the type used in the catch clause, so the output is "NotFoundException: user missing".
Php ExceptionsDifficulty 2
A team wants callers to be able to catch a "user not found" failure specifically, separately from other generic errors.
class UserNotFoundException extends Exception {}

function findUser(int $id): array {
    if ($id === 0) {
        throw new UserNotFoundException("user $id not found");
    }
    return ["id" => $id];
}

try {
    findUser(0);
} catch (UserNotFoundException $e) {
    echo "specific: " . $e->getMessage();
} catch (Exception $e) {
    echo "generic: " . $e->getMessage();
}

What is printed?
  • ageneric: user 0 not found
  • bFatal error: catch order is invalid here
  • cNothing is printed
  • dspecific: user 0 not found
Explanation:UserNotFoundException extends Exception, so a plain class Name extends Exception {} is a valid custom exception. Since the specific catch is listed first and matches, it runs before the generic one is ever considered.

Test yourself against the 3300-question Backend bank.

Start interview