This question arose from an issue raised by a user of this site.
In PHP expressions of the type are used:
if ($var == "Uno" || @intval($var1) == 1){
echo "Picked 1";
}
What does the @ used in mean @intval($var1)
?
Is there any equivalent in Java?
The @ before an expression or command works as an error suppressor . If the execution of the expression or command causes an error, the program will fail silently, continuing its course without anyone knowing that something went wrong.
Error suppression is a very bad practice , because it makes it impossible for you to debug your code. In practice, depending on the changes you make before the muted expression, the error you're suppressing may change and you won't have any way of knowing about it.
Using the typical example:
If that file does not exist, the program generates a type error
E_WARNING
, therefore, in the years 2000 - 2006 it was handled asIf the file does not exist, we simply check if
$handle
it is not equal tofalse
.So, some went further and to save trouble they wrote for example:
And by suppressing the errors of a function you suppress any errors that are inside it . You have no way of knowing if the
fopen
or any of the other commands failed. How to debug that?So, successively, the error suppressor was used to shut down entire sections of code that then didn't work and no one understood why.
The correct way to do this would instead be to catch the failure in an exception, using
try...catch
but...What do you do when the expression doesn't throw an exception but fails miserably?
Well, just don't let getHandle throw an E_WARNING, and don't suppress it either, but handle the error and raise an exception yourself:
And when calling
getHandle
you could wrap, now yes, in a blocktry / catch
.In this case you would know exactly why it failed. And if inside the function
getHandle
there are 10 things that can break, each one will be able to throw its own exception with a descriptive message.Do you notice the difference? Instead of letting an error occur, which breaks the flow, or suppress it with unpredictable results, you catch it and do something that changes the flow of the program. That way there are no programs that run and programs that crash, but only programs that run in a different way according to what they find.
In the current versions of the most popular frameworks, custom error handlers are used that convert errors into exceptions, so that every error ends up converted into an exception and you can use
try/catch
piecemeal.The dangerous thing about this is that an uncaught exception is always an error
E_FATAL
, so when using those ErrorHandlers you have to be very careful about your use of try...catch .Regarding your second question: more robust languages like Java, C# or Python do not have this defect of PHP regarding the duality between errors and exceptions (PHP has many dualities that are its main defect), so your range of possibilities is limited Catch the exception or cross your fingers.
In general, in this type of language, you can regulate the verbosity of the error to show fewer errors on the screen and send them directly to the log, so as not to expose the end user to information intended for debugging. The equivalent of suppressing the errors would be to set the error log to be written to
/dev/null
.I understand that there is no such thing in java, however there is one or another trick, in java 8 for example I have seen using lambda expressions set to something similar to the command pattern (command pattern)
check this answer
As Malcam has told you, the @ in php helps so that in case of an error it does not rise and causes an exception in our php code in case of an error, it is very similar to when you use a try{} catch(){} and your code is inside the try and it generates an error but in the catch you only leave the section inside it blank
and as you already know in java it is also possible to use try catch
Although it goes against good programming practices leaving an empty catch is sometimes the best solution while you find the right one :D
PS: I don't know of a direct equivalent in java for the function that @ performs in php.
http://php.net/manual/en/language.operators.errorcontrol.php
http://php.net/manual/en/language.exceptions.php