function inverso($x) {
if (!$x) {
throw new Exception('Zero division.');
}
return 1/$x;
}
try {
echo inverso(5) . "
";
echo inverso(0) . "
";
} catch (Exception $e) {
echo 'and the error is: ', $e->getMessage(), "
";
}
<?php
#We use exceptions to handle errors in PHP
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
echo divide(5, 0);
?>
<?php
function test() {
try {
throw new Exception('foo');
} catch (Exception $e) {
return 'catch';
} finally {
return 'finally';
}
}
echo test();
?>
try {
// call a success/error/progress handler
} catch (Throwable $e) { // For PHP 7
// handle $e
} catch (Exception $e) { // For PHP 5
// handle $e
}
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division durch Null.');
}
return 1/$x;
}
try {
echo inverse(5) . "
";
echo inverse(0) . "
";
} catch (Exception $e) {
echo 'Exception abgefangen: ', $e->getMessage(), "
";
}
// Ausführung fortsetzen
echo "Hallo Welt
";
?>
public function search(Request $request)
{
try {
$user = $this->userService->search($request->input('user_id'));
} catch (ModelNotFoundException $exception) {
return back()->withError($exception->getMessage())->withInput();
}
return view('users.search', compact('user'));
}
<?php
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
echo divide(5, 0);
?>