Complete the code to catch an exception.
<?php try { // code that may throw an exception } catch ([1] $e) { echo $e->getMessage(); } ?>
The Exception class is used to catch exceptions in PHP.
Complete the code to catch a specific exception type.
<?php try { // code that may throw an exception } catch ([1] $e) { echo 'Invalid argument: ' . $e->getMessage(); } ?>
InvalidArgumentException is used to catch invalid argument errors specifically.
Fix the error in the catch block to catch multiple exceptions separately.
<?php try { // code that may throw exceptions } catch (InvalidArgumentException $e) { echo 'Invalid argument: ' . $e->getMessage(); } catch ([1] $e) { echo 'Runtime error: ' . $e->getMessage(); } ?>
The second catch block should catch RuntimeException to handle runtime errors separately.
Fill both blanks to catch multiple exceptions and handle them differently.
<?php try { // code that may throw exceptions } catch ([1] $e) { echo 'Argument error: ' . $e->getMessage(); } catch ([2] $e) { echo 'General error: ' . $e->getMessage(); } ?>
The first catch block handles InvalidArgumentException, the second handles the general Exception.
Fill all three blanks to catch multiple exceptions and print different messages.
<?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(); } ?>
The code catches InvalidArgumentException, then RuntimeException, then any other Exception.
