0
0
PHPprogramming~10 mins

Throwing exceptions in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to throw an exception with the message "Error occurred".

PHP
<?php
throw new [1]("Error occurred");
?>
Drag options to blanks, or click blank then click option'
AException
BError
CThrowable
DRuntimeException
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class that does not exist or is not throwable.
Forgetting to use the 'new' keyword before the exception class.
2fill in blank
medium

Complete the code to throw a RuntimeException with the message "Runtime error".

PHP
<?php
throw new [1]("Runtime error");
?>
Drag options to blanks, or click blank then click option'
AException
BErrorException
CRuntimeException
DLogicException
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic Exception instead of a specific RuntimeException.
Using an unrelated exception class.
3fill in blank
hard

Fix the error in the code to correctly throw an exception with the message "Invalid input".

PHP
<?php
throw [1]("Invalid input");
?>
Drag options to blanks, or click blank then click option'
Anew Exception
BException
CError
Dnew Error
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the 'new' keyword before the exception class.
Using the exception class name without parentheses.
4fill in blank
hard

Fill both blanks to throw an exception with the message "File not found" using the ErrorException class.

PHP
<?php
throw new [1]("File not found", [2]);
?>
Drag options to blanks, or click blank then click option'
AErrorException
B404
C0
DException
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong exception class.
Using an invalid error code.
5fill in blank
hard

Fill all three blanks to throw a LogicException with the message "Invalid operation", code 100, and previous exception stored in $prev.

PHP
<?php
throw new [1]("Invalid operation", [2], [3]);
?>
Drag options to blanks, or click blank then click option'
AException
B100
C$prev
DLogicException
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong exception class.
Mixing up the order of arguments.
Not passing the previous exception variable.