What is the main purpose of exceptions in PHP?
A
To create cookies B
To handle errors gracefully C
To open files D
To start sessions
Analysis & Theory
Exceptions are used to handle errors in a structured and graceful way.
Which keyword is used to throw an exception in PHP?
A
raise B
throw C
except D
error
Analysis & Theory
The `throw` keyword is used to throw an exception in PHP.
Which block is used to handle an exception in PHP?
A
if...else B
try...except C
try...catch D
error...catch
Analysis & Theory
Exceptions are handled using `try...catch` blocks in PHP.
What happens if an exception is thrown but not caught?
A
Script continues normally B
Script ignores the exception C
Script terminates with a fatal error D
Exception is stored in a log
Analysis & Theory
If an exception is not caught, PHP terminates the script with a fatal error.
What is the base class for all exceptions in PHP?
A
BaseException B
Error C
Throwable D
Exception
Analysis & Theory
`Exception` is the base class for all exceptions in PHP.
Which block is always executed whether an exception is thrown or not?
A
catch B
throw C
try D
finally
Analysis & Theory
The `finally` block always executes regardless of whether an exception was thrown or caught.
What is the output of:
```
try {
throw new Exception('Error occurred');
} catch (Exception $e) {
echo $e->getMessage();
}
```
A
Nothing B
Error occurred C
Exception Error D
Fatal Error
Analysis & Theory
The output is `Error occurred` because `getMessage()` retrieves the message from the thrown exception.
Which method retrieves the exception code?
A
getError() B
getCode() C
getMessage() D
errorCode()
Analysis & Theory
`getCode()` retrieves the exception code associated with the thrown exception.
Which interface do all exceptions and errors implement starting from PHP 7?
A
Throwable B
Catchable C
ErrorHandler D
Exceptionable
Analysis & Theory
Starting from PHP 7, both `Exception` and `Error` implement the `Throwable` interface.
What happens if there are multiple catch blocks after a try block?
A
All catch blocks run B
Only the first matching catch block runs C
All are ignored D
Only the last catch block runs
Analysis & Theory
Only the first catch block matching the thrown exception type is executed.