-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherr_handling.php
More file actions
92 lines (74 loc) · 2.04 KB
/
err_handling.php
File metadata and controls
92 lines (74 loc) · 2.04 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html>
<head>
<title>Error Handling</title>
</head>
<?php
class customException extends Exception {
public function errorMessage(){
// error message
$errorMsg = 'Error on line ' . $this->getLine() . ' in ' . $this->getFile()
. ': <b>' . $this->getMessage() . '</b> is not a valid email address!';
return $errorMsg;
}
}
// if(!file_exists("welcome.txt")){
// echo "File does not exist!";
// } else {
// $file = fopen("welcome.txt", "r");
// }
// // set error function
// function customError($errno, $errstr) {
// echo "<br>Error:</br> [$errno] $errstr<br>";
// echo "Webmaster has been notified";
// error_log("Error: [$errno] $errstr", 1,"obengjohnsonboateng@gmail.com", "From: webmaster@example.com");
// }
// // set error handler
// set_error_handler("customError", E_USER_WARNING);
// // trigger error
// $test = 2;
// if($test > 1) {
// trigger_error("Value must be 1 or below!", E_USER_WARNING);
// }
// Exception handling
// function checkNum($number) {
// if($number > 1) {
// throw new Exception("Value must be 1 or below");
// }
// return true;
// }
// // trigger exception in a try block
// try{
// checkNum(2);
// echo "If I see this, the number is 1 or below!";
// } catch(Exception $e) {
// echo "Message: " . $e->getMessage();
// }
$email = 'someone@example...com';
try {
try {
// if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
// throw new customException($email);
// }
if(strpos($email, "example") !== FALSE){
throw new Exception($email);
}
} catch(Exception $e) {
throw new customException($email);
}
// catch(Exception $e) {
// echo $e->getMessage();
// }
}
catch(customException $e){
echo $e->errorMessage();
}
function myException($exception){
echo "<br>Exception</b> " . $exception->getMessage();
}
set_exception_handler("myException");
throw new Exception("Uncaught exception occurred!");
?>
<body>
</body>
</html>