PHP.mk документација

DomainException

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

class.domainexception.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека class.domainexception.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
DomainException

Референца за `class.domainexception.php` со подобрена типографија и навигација.

class.domainexception.php

Класата DomainException

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Вовед

Исклучок што се фрла ако вредноста не се придржува до дефинирана валидна област на податоци.

Синопсис на класата

class DomainException extends LogicException {
/* Наследени својства */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
/* Наследени методи */
public Exception::__construct(string $message = "", int $code = 0, ?Проверува тврдење $previous = null)
final public Exception::getCode(): int
final public Exception::getFile(): string
final public Exception::getLine(): int
final public Exception::getTrace(): array
}

Белешки од корисници 4 белешки

[email protected]
пред 14 години
<?php
function renderImage($imageResource, $imageType)
{
  switch ($imageType) {
  case 'jpg':
  case 'jpeg':
    header('Content-type: image/jpeg');
    imagejpeg($imageResource);
    break;
  case 'png':
    header('Content-type: image/png');
    imagepng($imageResource);
    break;
  default:
    throw new DomainException('Unknown image type: ' . $imageType);
    break;
  }
  imagedestroy($imageResource);
}
?>
ja2016 на wir точка pl
пред 9 години
I think this kind of exception is perfect to throw when expected the  type of parameter, value etc. is good, but its value is out of domain. Look at RangeException:
>>Exception thrown to indicate range errors during program execution. Normally this means there was an arithmetic error other than under/overflow. This is the runtime version of DomainException.<<
So, this kind of exception is designed for logic error

When datatype is wrong, the better way is throwing InvalidArgumentException. 

<?php
// Here, use InvalidArgumentException
function media($x) {
    switch ($x) {
        case image:
            return 'PNG';
        break;
        case video:
            return 'MP4';
        break;
        default:
            throw new InvalidArgumentException ("Invalid media type!");
    }
}?>
This is completly diffirent situation than this:
<?php
// Here, use DomainException
$object = new Library ();
try {
    $object->allocate($x);
} catch (toFewMin $e) {
    throw new DomainException ("Minimal value to allocate is too high").
}
?>
The simillar situation, but problem occurs during runtime:
<?php
class library {
    function allocate($x) {
        if ($x<1000)
            throw new RangeException ("Value is too low!")
    }
}
?>
Summary: DomainException corresponds to RangeException and we should use them in simillar situations.  But first exception is designed to use when we are sure the problem is with our project, third-part elements etc. (simply: logical error), the second way is designed to use when we are sure the problem is with input data or environment (simply: runtime error).
[email protected]
пред 11 години
<?php

function divide($divident, $divisor) {
    if(!is_numeric($divident) || !is_numeric($divisor)) {
        throw new InvalidArgumentException("Function accepts only numeric values");
    }
    if($divisor == 0) {
        throw new DomainException("Divisor must not be zero");
    }
    return $divident / $divisor;
}
Крстосувач
пред 8 години
Quote: "In data management and database analysis, a data domain refers to all the values which a data element may contain."

Source: https://en.wikipedia.org/wiki/Data_domain

This exception has confused me a bit, DataDomainException, or DataTypeException may have been more descriptive.
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.