Exceptions
Recover with Default
A catch block can choose a default value and let the program continue.
fallback
Recovery usually means producing a clear fallback value, not hiding the failure.
Recover with Default
recover_default.php
Replay: real traced execution (multi-file project)
<?php
function parseCount(string $raw): int
{
if (!ctype_digit($raw)) {
throw new Exception("not a number");
}
return (int) $raw;
}
$raw = "12";
try {
$count = parseCount($raw);
$source = "input";
} catch (Exception $error) {
$count = 0;
$source = "default";
}
echo "raw=" . $raw . "\n";
echo "count=" . $count . "\n";
echo "source=" . $source . "\n";
<?php
function parseCount(string $raw): int
{
if (!ctype_digit($raw)) {
throw new Exception("not a number");
}
return (int) $raw;
}
$raw = "bad";
try {
$count = parseCount($raw);
$source = "input";
} catch (Exception $error) {
$count = 0;
$source = "default";
}
echo "raw=" . $raw . "\n";
echo "count=" . $count . "\n";
echo "source=" . $source . "\n";
<?php
function parseCount(string $raw): int
{
if (!ctype_digit($raw)) {
throw new Exception("not a number");
}
return (int) $raw;
}
$raw = "5";
try {
$count = parseCount($raw);
$source = "input";
} catch (Exception $error) {
$count = 0;
$source = "default";
}
echo "raw=" . $raw . "\n";
echo "count=" . $count . "\n";
echo "source=" . $source . "\n";
$raw ← 12
11$raw→ 12 = "12"; //@raw="bad", "5"1213try {14 $count = parseCount($raw12);15 $source = "input";function parseCount(string $raw): int
1<?php2function parseCount(string $raw12): int3{4 if (!ctype_digit($raw)) {5 throw new Exception("not a number");6 }78 return (int) $raw12;9}$count ← 12, $source ← input
13try {14 $count→ 12 = parseCount($raw12);15 $source→ input = "input";16} catch (Exception $error) {17 $count = 0;18 $source = "default";19}2021echo "raw=" . $raw12 . "\n";22echo "count=" . $count12 . "\n";23echo "source=" . $sourceinput . "\n";outputraw=12 count=12 source=input
$raw ← bad
11$raw→ bad = "bad";1213try {14 $count = parseCount($rawbad);15 $source = "input";function parseCount(string $raw): int
1<?php2function parseCount(string $rawbad): int3{4 if (!ctype_digit($raw)) {if (!ctype_digit($raw))
3{4 if (!ctype_digit($rawbad)) {5 throw new Exception("not a number");6 }echo "raw=" . $raw . " ";
16} catch (Exception $error) {17 $count = 0;18 $source = "default";19}2021echo "raw=" . $rawbad . "\n";22echo "count=" . $count0 . "\n";23echo "source=" . $sourcedefault . "\n";outputraw=bad count=0 source=default
$raw ← 5
11$raw→ 5 = "5";1213try {14 $count = parseCount($raw5);15 $source = "input";function parseCount(string $raw): int
1<?php2function parseCount(string $raw5): int3{4 if (!ctype_digit($raw)) {5 throw new Exception("not a number");6 }78 return (int) $raw5;9}$count ← 5, $source ← input
13try {14 $count→ 5 = parseCount($raw5);15 $source→ input = "input";16} catch (Exception $error) {17 $count = 0;18 $source = "default";19}2021echo "raw=" . $raw5 . "\n";22echo "count=" . $count5 . "\n";23echo "source=" . $sourceinput . "\n";outputraw=5 count=5 source=input