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
function test() {
try {
throw new Exception('foo');
} catch (Exception $e) {
return 'catch';
} finally {
return 'finally';
}
}
echo test();
?>
<?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'));
}