0
0
PHPprogramming~10 mins

Multiple catch blocks 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 catch an exception.

PHP
<?php
try {
    // code that may throw an exception
} catch ([1] $e) {
    echo $e->getMessage();
}
?>
Drag options to blanks, or click blank then click option'
AThrowable
BException
CError
DCatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Catch' instead of an exception class.
Using 'Error' when catching exceptions.
2fill in blank
medium

Complete the code to catch a specific exception type.

PHP
<?php
try {
    // code that may throw an exception
} catch ([1] $e) {
    echo 'Invalid argument: ' . $e->getMessage();
}
?>
Drag options to blanks, or click blank then click option'
AErrorException
BException
CRuntimeException
DInvalidArgumentException
Attempts:
3 left
💡 Hint
Common Mistakes
Using the generic Exception class instead of the specific one.
3fill in blank
hard

Fix the error in the catch block to catch multiple exceptions separately.

PHP
<?php
try {
    // code that may throw exceptions
} catch (InvalidArgumentException $e) {
    echo 'Invalid argument: ' . $e->getMessage();
} catch ([1] $e) {
    echo 'Runtime error: ' . $e->getMessage();
}
?>
Drag options to blanks, or click blank then click option'
ARuntimeException
BException
CError
DThrowable
Attempts:
3 left
💡 Hint
Common Mistakes
Using the generic Exception class again.
Using Error which is not an exception.
4fill in blank
hard

Fill both blanks to catch multiple exceptions and handle them differently.

PHP
<?php
try {
    // code that may throw exceptions
} catch ([1] $e) {
    echo 'Argument error: ' . $e->getMessage();
} catch ([2] $e) {
    echo 'General error: ' . $e->getMessage();
}
?>
Drag options to blanks, or click blank then click option'
AInvalidArgumentException
BRuntimeException
CException
DError
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of exceptions.
Using Error instead of Exception.
5fill in blank
hard

Fill all three blanks to catch multiple exceptions and print different messages.

PHP
<?php
try {
    // code that may throw exceptions
} catch ([1] $e) {
    echo 'Argument error: ' . $e->getMessage();
} catch ([2] $e) {
    echo 'Runtime error: ' . $e->getMessage();
} catch ([3] $e) {
    echo 'Other error: ' . $e->getMessage();
}
?>
Drag options to blanks, or click blank then click option'
AInvalidArgumentException
BRuntimeException
CException
DError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching Exception before specific exceptions.
Using Error instead of Exception.