<?php declare(strict_types = 1);
class Test
{
/**
* @throws Exception
*/
public function mayThrow(): int
{
if (mt_rand() / mt_getrandmax() >= 0.5) {
throw new Exception();
} else {
return 1;
}
}
public function mayAlsoThrow(): int
{
return $this->mayThrow();
}
public function testCatchException(): void
{
try {
$foo = $this->mayThrow();
$foo += $this->mayAlsoThrow();
var_dump($foo);
} catch (Exception) {
var_dump($foo ?? 0);
}
}
public function testCatchOnlyImplicitException(): void
{
try {
$foo = $this->mayAlsoThrow();
$foo += $this->mayAlsoThrow();
var_dump($foo);
} catch (Exception) {
var_dump($foo ?? 0);
}
}
public function testCatchThrowable(): void
{
try {
$foo = $this->mayThrow();
$foo += $this->mayAlsoThrow();
var_dump($foo);
} catch (Throwable) {
var_dump($foo ?? 0);
}
}
}
(new Test())->testCatchException();
- Output for 8.0.5 - 8.0.6, 8.0.9, 8.0.15, 8.0.17, 8.0.28, 8.1.10, 8.1.15, 8.1.23, 8.2.2 - 8.2.3, 8.2.10
- int(2)
- Output for 8.0.1 - 8.0.3, 8.0.7, 8.0.12 - 8.0.14, 8.0.16, 8.0.18, 8.0.20 - 8.0.21, 8.0.23, 8.0.25 - 8.0.27, 8.1.1 - 8.1.6, 8.1.11 - 8.1.12, 8.1.14, 8.1.16 - 8.1.22, 8.2.0 - 8.2.1, 8.2.4 - 8.2.9
- int(0)
- Output for 8.0.8, 8.0.10 - 8.0.11, 8.0.19, 8.0.22, 8.0.24, 8.0.29 - 8.0.30, 8.1.0, 8.1.7 - 8.1.9, 8.1.13
- int(1)
preferences:
87.77 ms | 403 KiB | 69 Q