What are the native exceptions of PHP?

Asked

Viewed 505 times

11

Where it is possible to check all exceptions (native) that can be released by PHP? I searched and only found ways to treat with try/catch.

3 answers

13


As native exceptions sane:

  • BadFunctionCallException

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

  • BadMethodCallException

    Occurs when a callback refers to a method undefined 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 a length (length) is invalid.

  • LogicException

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

  • OutOfBoundsException

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

  • OutOfRangeException

    Exception thrown when an invalid index is prompted. This represents errors that must be detected at build time.

  • OverflowException

    Exception thrown when adding an element to a container full.

  • RangeException

    Exception thrown to indicate interval errors (crease) during the execution of the program. Normally 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 runtime.

  • UnderflowException

    Exception thrown when performing an invalid operation on a container empty, 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.

13

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

Here is the list of errors that can be captured in PHP 7:

Error

It’s some kind of mistake, like a Warning or a Fatal Error. It is the base class for all other errors that will be demonstrated below.

Arithmeticerror

is launched when an error occurs during the execution of mathematical operations. In PHP 7.0, these errors include trying to run a Bitshift for a negative value, and any call to intDiv() that would result in a value outside the possible limits of an integer.

Divisionbyzeroerror

Derivative of ArithmeticError, as its name says, it is launched when you try to do a split operation by 0.

Parseerror

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

Typeerror

This error is thrown when you have passed arguments to functions whose type is the unhealthy, 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 launch of the same when you an invalid number of arguments for a function.

Assertionerror

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

These errors are exceptions?

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

Thus:

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

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

  • 1

    Good complement Wallacemaxters

6

Of the native exceptions, it is 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.