In some PHP built-in functions (such as mkdir
), adding @ in front can mask the errors thrown by the function
So how does a custom function achieve this?
I tried throw new Exception
and trigger_error
and they couldn’t be blocked by @
It’s like this, for example, I want to write a JsonFile::get()
method that throws an error when the file does not exist, throws an error when the path is illegal, throws an error when decoding fails, and returns false
When the user cares about the error message, he or she can capture and handle it by himself; when the user does not care about the error message and only wants to get the file content or false (failure), he can call @JsonFile::get()
like this
Mainly for convenience. How to implement such a custom method (or function)?
Or maybe my way of thinking is not good? Is there a convenient and reasonable implementation method?
In some PHP built-in functions (such as mkdir
), adding @ in front can mask the errors thrown by the function
So how does a custom function achieve this?
I tried throw new Exception
and trigger_error
and they couldn’t be blocked by @
It’s like this, for example, I want to write a JsonFile::get()
method that throws an error when the file does not exist, throws an error when the path is illegal, throws an error when decoding fails, and returns false
When the user cares about the error message, he or she can capture and handle it by himself; when the user does not care about the error message and only wants to get the file content or false (failure), he can call @JsonFile::get()
like this
Mainly for convenience. How to implement such a custom method (or function)?
Or maybe my way of thinking is not good? Is there a convenient and reasonable implementation method?
First of all, make sure the syntax is correct, and then it seems to have nothing to do with throw new Exception and trigger_error. Here is the code:
<code><?php function test($abc){ mysql_connect("localhost", "用户名错误", "密码错误"); } function test2($abc){ mysql_connect("localhost", "用户名错误", "密码错误"); } test("abc"); @test2("abc"); ?></code>
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'wrong username'@'localhost' (using password: YES) in D:Projectsns1.php on line 4
test2 does not throw Warning.