1.2.299. Could Use Try

Some commands may raise exceptions. It is recommended to use the try/catch block to intercept those exceptions, and process them.

  • / : DivisionByZeroError

  • % : DivisionByZeroError

  • intdiv() : DivisionByZeroError, ArithmeticError

  • << : ArithmeticError

  • >> : ArithmeticError

  • Phar\:\:mungserver : PharException

  • Phar\:\:webphar : PharException

Some exceptions have an extra analysis, due to special detection condition : ParseError, with eval() and DivisionByZeroError.

<?php

function division(int $a, int $b) {
     // This expression might generate a DivisionByZeroError, and require a try/catch for error handling purposes.
     return $a / $b;
}

?>

See also Predefined Exceptions and PharException.

1.2.299.1. Suggestions

  • Add a try/catch clause around those commands

  • Add a check on the values used with those operator : for example, check a dividend is not 0, or a bitshift is not negative

1.2.299.2. Specs

Short name

Exceptions/CouldUseTry

Rulesets

All, Suggestions

Exakat since

1.5.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

try-catch, exception, arithmeticerror, divisionbyzeroerror, imagickexception, imagickpixelexception, invalidargumentexception, jsonexception, mysqli_sql_exception, pdoexception, pharexception, reflectionexception, svmexception, typerror, unexpectedvalueexception

Examples

Mautic

Related rule

eval() Without Try, Check Division By Zero

Available in

Entreprise Edition, Exakat Cloud