-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.php
More file actions
52 lines (44 loc) · 1.15 KB
/
exception.php
File metadata and controls
52 lines (44 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/*
* Copyright (C) 2024-2026 Katarzyna Krasińska
* PHP.lab - https://github.com/katheroine/php.lab
* Licensed under GPL-3.0 - see LICENSE.md
*/
class SomeException extends Exception
{
public mixed $value;
function __construct(mixed $value)
{
$this->value = $value;
$this->message = "Value has beign given.";
}
}
class OtherException extends SomeException
{
function __construct(int $number)
{
$this->value = $number;
$this->message = "Number has beign given.";
}
}
function someRiskySituation(): void
{
$input = readline("Input: ");
if (empty($input)) {
print("No exception.\n");
} elseif (!is_numeric($input)) {
throw new SomeException($input);
} else {
throw new OtherException($input);
}
}
try {
someRiskySituation();
} catch (OtherException $e) {
print("Some exception: " . $e->getMessage() . " (" . $e->value . ")\n");
} catch (SomeException $e) {
print("Other exception: " . $e->getMessage() . " (" . $e->value . ")\n");
} finally {
print("Will always execute.\n");
}
print("Will also execute (due to exception has been catched).\n");