-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.php
executable file
·60 lines (49 loc) · 1.56 KB
/
exceptions.php
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
53
54
55
56
57
58
59
60
#!/usr/bin/php8.2
<?php
class NumberValueException extends Exception {
public int $number;
// public string $message;
}
class ZeroException extends NumberValueException {
function __construct(int $number) {
$this->number = $number;
$this->message = "0 number has beign given.";
}
}
class OneException extends NumberValueException {
function __construct(int $number) {
$this->number = $number;
$this->message = "1 number has beign given.";
}
}
class ThousandException extends NumberValueException {
function __construct(int $number) {
$this->number = $number;
$this->message = "1000 number has beign given.";
}
}
function draw_number() {
$number = readline("Give some number: ");
if ($number == 0) {
throw new ZeroException($number);
} else if ($number == 1) {
throw new OneException($number);
} else if ($number == 1000) {
throw new ThousandException($number);
} else if ($number == 10000) { // Unhandled exception.
throw new NumberValueException();
}
}
print("Program begin...\n");
try {
print("Risky code...\n");
$number = draw_number();
print("Given number " . $number . " didn't case exception throwing.\n");
} catch (ZeroException $e) {
print("CASE 1: " . $e->getMessage() . " (" . $e->number . ")\n");
} catch (OneException $e) {
print("CASE 2: " . $e->getMessage() . " (" . $e->number . ")\n");
} catch (ThousandException $e) {
print("CASE 3: " . $e->getMessage() . " (" . $e->number . ")\n");
}
print("Program end...\n");