What are the native PHP exceptions?

11

Where can I check all the exceptions (native) that can be launched by PHP? I searched and found only forms of treatment with try/catch .

    
asked by anonymous 14.04.2015 / 16:57

3 answers

13

native exceptions are:

  • BadFunctionCallException

      

    Occurs when a callback refers to an undefined function or if some arguments are missing.

  • BadMethodCallException

      

    Occurs when a callback refers to an method that is not defined or if some arguments are missing.

  • DomainException

      

    Exception thrown when a value does not adhere to a valid data domain.

  • InvalidArgumentException

      

    Exception thrown if an argument is not of the expected type.

  • LengthException

      

    Exception thrown if length ( length ) is invalid.

  • LogicException

      

    Exception representing error in program logic. This type of exception should lead directly to a fix in your code.

  • OutOfBoundsException

      

    Exception thrown when a value is not a valid key. This represents errors that can not be detected at compile time.

  • OutOfRangeException

      

    Exception thrown when an invalid index was requested. This represents errors that should be detected at compile time.

  • OverflowException

      

    Exception thrown when adding an element to a container full.

  • RangeException

      

    Exception thrown to indicate interval errors ( range ) during program execution. Usually this means that there was an arithmetic error except under / overflow . This is the runtime version of DomainException .

  • RuntimeException

      

    Exception thrown if an error occurs that can only be found at run time.

  • UnderflowException

      

    Exception thrown when performing an invalid operation on an empty container , such as removing an element.

  • UnexpectedValueException

      

    Exception thrown when a value does not match a set of values. Usually, this happens when a function calls another function and expects the return value to be of a certain type.

14.04.2015 / 18:02
13

Regarding PHP 7, exceptions have been implemented in common PHP errors such as Warning , Fatal Error and Parse Error .

Here's the list of errors that can be caught in PHP 7:

Error

It's an error, such as Warning or Fatal Error . It is the base class for all other errors that will be demonstrated next.

ArithmeticError

is thrown when an error occurs while performing math operations. In PHP 7.0, these errors include trying to execute a BitShift by a negative value, and any call to intDiv () that would result in a value outside the possible bounds of an integer.

DivisionByZeroError

Derived from ArithmeticError , as the name itself says, is thrown when you try to do a split operation by 0.

ParseError

It is thrown when an error occurs during parsing PHP code, for example, as in a eval that is called. I think another case is when you include another PHP script and there are errors in it.

TypeError

This error is thrown when you pass arguments to functions whose type is inspected, when the return is different from what is defined in the function. In the manual it also says that in strict mode there is the release of the same when you an invalid number of arguments to a function.

AssertionError

It is thrown when a statement made by assert () returns false.

Are these errors exceptions?

It seems to me that PHP resolved to treat errors differently from exceptions. All of these errors described above do not inherit the traditional Exception class from PHP. To capture them, you must add the Throwable interface in the catch option.

So:

try{
    $result = 59 / 0;
} catch (Throwable $e) {
   var_dump($e instanceof DivisionByZeroError); // bool(true)
}

In PHP 7, both exceptions and errors implement the Throwable interface.

    
08.01.2016 / 12:50
6

Of the native exceptions, it's good to know that some extend others, forming the next hierarchy :

  • LogicException (extends Exception)
    • BadFunctionCallException
      • BadMethodCallException
    • DomainException
    • InvalidArgumentException
    • LengthException
    • OutOfRangeException
  • RuntimeException (extends Exception)
    • OutOfBoundsException
    • OverflowException
    • RangeException
    • UnderflowException
    • UnexpectedValueException

It is interesting to create more specific exceptions in the domain of your application, extending the existing exceptions.

<?php 

class QueroBatataException extends UnexpectedValueException { }

class QueroCebolaException extends UnexpectedValueException { }

function batata($nomeLegume) {
    if ($nomeLegume !== 'batata') {
        throw new QueroBatataException('Me dê batata');
    }

    echo 'Huuun Batata!' . PHP_EOL;
}


function cebola($nomeLegume) {
    if ($nomeLegume !== 'cebola') {
        throw new QueroCebolaException('Me dê cebola');
    }

    echo 'Huuun Cebola!' . PHP_EOL;
}

So in your code, you can do this:

<?php

$legume = 'batata';

try { 
    batata($legume);
    cebola($legume);
} catch (UnexpectedValueException $e) {
    echo $e->getMessage();
}

// Saída:
// Huuun Batata! 
// Me dê cebola

See the running code .

    
30.08.2016 / 23:58