In some PHP built-in functions (such asmkdir
), adding @ in front can mask the errors thrown by the function
So how does a custom function achieve this?
I triedthrow new Exception
andtrigger_error
and they couldn’t be blocked by@
It’s like this, for example, I want to write aJsonFile::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 returnsfalse
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 asmkdir
), adding @ in front can mask the errors thrown by the function
So how does a custom function achieve this?
I triedthrow new Exception
andtrigger_error
and they couldn’t be blocked by@
It’s like this, for example, I want to write aJsonFile::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 returnsfalse
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:
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.