Recall & Review
beginner
What is an exception in PHP?
An exception is an object that represents an error or unexpected behavior during program execution. It can be "thrown" to signal a problem and "caught" to handle it gracefully.
Click to reveal answer
beginner
How do you throw an exception in PHP?
Use the
throw keyword followed by a new Exception object, like this: throw new Exception('Error message');Click to reveal answer
beginner
What is the purpose of a try-catch block?
A
try block contains code that might throw an exception. The catch block handles the exception if one is thrown, preventing the program from crashing.Click to reveal answer
intermediate
What happens if an exception is thrown but not caught?
If an exception is thrown and not caught, PHP stops the program and shows a fatal error message. This is like the program saying, "I don't know how to handle this problem."
Click to reveal answer
intermediate
Can you throw custom exceptions in PHP?
Yes! You can create your own exception classes by extending the built-in
Exception class. This helps you handle specific error types more clearly.Click to reveal answer
Which keyword is used to throw an exception in PHP?
✗ Incorrect
The
throw keyword is used to throw an exception.What does a catch block do?
✗ Incorrect
A catch block handles exceptions thrown inside the try block.
What happens if an exception is not caught?
✗ Incorrect
If an exception is not caught, PHP stops execution and shows a fatal error.
How do you create a custom exception class?
✗ Incorrect
Custom exceptions are created by extending the built-in Exception class.
Which of these is the correct way to throw an exception with message 'Error'?
✗ Incorrect
The correct syntax is
throw new Exception('Error');Explain how throwing and catching exceptions works in PHP.
Think about how you signal a problem and then handle it.
You got /5 concepts.
Describe why you might want to create a custom exception class.
Consider how different problems might need different responses.
You got /4 concepts.