0
0
PHPprogramming~5 mins

Throwing exceptions in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aerror
Bcatch
Ctry
Dthrow
What does a catch block do?
AHandles an exception
BThrows an exception
CStarts the program
DEnds the program
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
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
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');
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.