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?
Aerror
Bcatch
Ctry
Dthrow
✗ Incorrect
The throw keyword is used to throw an exception.
What does a catch block do?
AHandles an exception
BThrows an exception
CStarts the program
DEnds the program
✗ Incorrect
A catch block handles exceptions thrown inside the try block.
What happens if an exception is not caught?
AProgram continues normally
BPHP shows a warning but continues
CPHP stops with a fatal error
DException is ignored
✗ Incorrect
If an exception is not caught, PHP stops execution and shows a fatal error.
How do you create a custom exception class?
ABy writing a try block
BBy extending the Exception class
CBy using the throw keyword
DBy calling the catch block
✗ 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'?
Athrow new Exception('Error');
Bthrow Exception('Error');
Ccatch new Exception('Error');
Dtry new Exception('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.
Practice
(1/5)
1. What does the throw statement do in PHP?
easy
A. It catches an exception to handle an error.
B. It creates and signals an exception to indicate an error.
C. It defines a new function for error handling.
D. It stops the script without any error message.
Solution
Step 1: Understand the purpose of throw
The throw statement is used to create and signal an exception when an error occurs.
Step 2: Differentiate from other error handling keywords
catch handles exceptions, but throw actually creates and signals them.
Final Answer:
It creates and signals an exception to indicate an error. -> Option B
2. Which of the following is the correct syntax to throw an exception with the message "Error occurred" in PHP?
easy
A. throw new Exception("Error occurred");
B. throw Exception("Error occurred");
C. throw Exception new("Error occurred");
D. throw new Exception(Error occurred);
Solution
Step 1: Recall the syntax for throwing exceptions
In PHP, you must use throw new Exception("message") to throw an exception with a message.
Step 2: Check each option for syntax correctness
Only throw new Exception("Error occurred"); matches the correct syntax exactly. The other options either omit the new keyword, have the wrong order of keywords, or lack quotes around the message.
Final Answer:
throw new Exception("Error occurred"); -> Option A
Quick Check:
Correct syntax = throw new Exception(message) [OK]
Hint: Use 'throw new Exception("message")' exactly [OK]
Common Mistakes:
Omitting the 'new' keyword
Not quoting the message string
Wrong order of keywords
3. What will be the output of the following PHP code?
try {
throw new Exception("Oops!");
echo "No error";
} catch (Exception $e) {
echo $e->getMessage();
}
medium
A. No error
B. Fatal error
C. Exception thrown but no output
D. Oops!
Solution
Step 1: Analyze the try block
The code throws an exception with message "Oops!" before the echo statement, so "No error" is never printed.
Step 2: Understand the catch block behavior
The catch block catches the exception and echoes the message from the exception, which is "Oops!".
Final Answer:
Oops! -> Option D
Quick Check:
Exception message printed = Oops! [OK]
Hint: Exception stops try block; catch prints message [OK]
C. Method message() does not exist; should use getMessage().
D. Catch block syntax is incorrect.
Solution
Step 1: Check the catch block method call
The Exception class has a method getMessage() to get the error message, not message().
Step 2: Verify other syntax elements
The semicolon after throw is present, Exception class is built-in, and catch syntax is correct.
Final Answer:
Method message() does not exist; should use getMessage(). -> Option C
Quick Check:
Use getMessage() to get exception text [OK]
Hint: Use getMessage() method to read exception message [OK]
Common Mistakes:
Calling non-existent message() method
Missing semicolons (not in this case)
Incorrect catch syntax
5. You want to throw an exception only if a variable $age is less than 18. Which code correctly does this and catches the exception to print its message "Too young"?
hard
A. try {
if ($age < 18) {
throw new Exception("Too young");
}
} catch (Exception $e) {
echo $e->getMessage();
}
B. try {
if ($age < 18) {
throw new Exception("Too young");
}
} catch (Exception $e) {
echo "Too young";
}
C. try {
if ($age < 18) {
throw Exception("Too young");
}
} catch (Exception $e) {
echo $e->getMessage();
}
D. if ($age < 18) {
throw new Exception("Too young");
} catch (Exception $e) {
echo $e->getMessage();
}
Solution
Step 1: Identify correct try-catch structure
The exception must be thrown inside a try block to be caught properly. try {
if ($age < 18) {
throw new Exception("Too young");
}
} catch (Exception $e) {
echo $e->getMessage();
} correctly wraps the throw inside try.
Step 2: Check exception throwing syntax and catch output
try {
if ($age < 18) {
throw new Exception("Too young");
}
} catch (Exception $e) {
echo $e->getMessage();
} uses correct syntax throw new Exception("Too young") and prints the exception message with echo $e->getMessage();.
Final Answer:
try { if ($age < 18) { throw new Exception("Too young"); } } catch (Exception $e) { echo $e->getMessage(); } -> Option A
Quick Check:
Throw inside try, catch prints message [OK]
Hint: Always throw exceptions inside try blocks to catch them [OK]
Common Mistakes:
Throwing outside try block
Missing 'new' keyword when throwing
Printing hardcoded message instead of exception message
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.