PHP / Java: What does the use of @ mean in PHP? Does Java have an equivalent?

Question:

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 @intval($var1) ?

Is there an equivalent in Java?

Answer:

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, running its course without anyone knowing that something went wrong.

The error suppressor 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 silenced expression, the error you are suppressing may change and you will not have a way to find out.

Using the typical example:

function getHandle(file) {

    ...algunos comandos...

    $handle = fopen(file,r);

    return $handle;
}

If that file does not exist, the program generates an error type E_WARNING , so in the years 2000 – 2006 it was handled in the same way

function getHandle(file) {

    ...algunos comandos...

    $handle = @fopen(file,r);

    return $handle;
}

If the file doesn't exist, we just check if $handle is not equal to false .

So, some went further and to save problems they wrote for example:

$handle = @getHandle('archivo.txt');

And by suppressing the errors of a function you suppress any errors that are within it . You have no way of knowing if the fopen or any of the other commands failed. How to debug that?

So, in succession, the error suppressor was used to hush up entire sections of the 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 does not throw an exception but fails miserably?

Well, just don't let getHandle throw an E_WARNING, nor suppress it, but handle the error and raise an exception yourself:

function getHandle(file) {

    ...algunos comandos...

    if(is_readable(file)) {
      $handle = fopen(file,r);

      ... otros comandos ...

      return $handle;

    } else {
     throw new \Exception('el archivo no existe');
    }
}

And when calling getHandle you could wrap, now yes, in a try / catch block.

try {
  $handle = getHandle('archivo.txt');
} catch (\Exception $e) {
    echo "La función ha fallado con mensaje: ".$e->getMessage();
}

In this case you would know exactly why it failed. And if within the getHandle function getHandle are 10 things that can be broken, each one will be able to throw its own exception with a descriptive message.

Do you realize the difference? Instead of letting an error occur, which interrupts the flow, or suppressing it with unpredictable results, you catch it and do something that changes the flow of the program. In this 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 turn errors into exceptions, so that every error ends up as an exception and you can use try/catch piecemeal.

The dangerous thing about this is that an E_FATAL exception is always an E_FATAL error, so when using those ErrorHandlers you have to be very meticulous with the use of try … catch.

Regarding your second question: more robust languages ​​such as Java, C # or Python do not have this PHP defect regarding the duality between errors and exceptions (PHP has many dualities that are its main defect), so your range of possibilities is limited to catch the exception or cross your fingers.

In general, in these types of languages ​​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 information to the end user for debugging. The equivalent of suppressing the errors would be to set the error log to be written to /dev/null .

Scroll to Top