Exceptions
Exception Message
An exception can carry a short message that explains the failure.
message
The message is data; the catch block decides whether to expose it.
Exception Message
exception_message.php
Replay: real traced execution (multi-file project)
<?php
$age = 17;
try {
if ($age < 18) {
throw new Exception("too young");
}
$message = "allowed";
} catch (Exception $error) {
$message = $error->getMessage();
}
echo "age=" . $age . "\n";
echo "message=" . $message . "\n";
<?php
$age = 12;
try {
if ($age < 18) {
throw new Exception("too young");
}
$message = "allowed";
} catch (Exception $error) {
$message = $error->getMessage();
}
echo "age=" . $age . "\n";
echo "message=" . $message . "\n";
<?php
$age = 25;
try {
if ($age < 18) {
throw new Exception("too young");
}
$message = "allowed";
} catch (Exception $error) {
$message = $error->getMessage();
}
echo "age=" . $age . "\n";
echo "message=" . $message . "\n";
$age ← 17
1<?php2$age→ 17 = 17; //@age=12, 25if ($age < 18)
4try {5 if ($age17 < 18) {6 throw new Exception("too young");7 }echo "age=" . $age . " ";
10} catch (Exception $error) {11 $message = $error->getMessage();12}1314echo "age=" . $age17 . "\n";15echo "message=" . $messagetoo young . "\n";outputage=17 message=too young
$age ← 12
1<?php2$age→ 12 = 12;if ($age < 18)
4try {5 if ($age12 < 18) {6 throw new Exception("too young");7 }echo "age=" . $age . " ";
10} catch (Exception $error) {11 $message = $error->getMessage();12}1314echo "age=" . $age12 . "\n";15echo "message=" . $messagetoo young . "\n";outputage=12 message=too young
$age ← 25, $message ← allowed
1<?php2$age→ 25 = 25;34try {5 if ($age < 18) {6 throw new Exception("too young");7 }89 $message→ allowed = "allowed";10} catch (Exception $error) {11 $message = $error->getMessage();12}1314echo "age=" . $age25 . "\n";15echo "message=" . $messageallowed . "\n";outputage=25 message=allowed