PHP handleiding

PHP TUIS PHP Inleiding PHP installeer PHP sintaksis PHP opmerkings PHP veranderlikes PHP Echo / Druk PHP datatipes PHP Strings PHP-nommers PHP Wiskunde PHP konstante PHP-operateurs PHP As...Anders...Elseif PHP skakelaar PHP-lusse PHP funksies PHP-skikkings PHP Superglobals PHP RegEx

PHP- vorms

PHP-vormhantering PHP-vorm validering PHP-vorm word vereis PHP-vorm URL/e-pos PHP-vorm voltooi

PHP Gevorderd

PHP datum en tyd PHP sluit in PHP-lêerhantering PHP-lêer oop/lees PHP-lêer skep/skryf PHP-lêer oplaai PHP-koekies PHP-sessies PHP filters PHP-filters Gevorderd PHP-terugbelfunksies PHP JSON PHP-uitsonderings

PHP OOP

PHP Wat is OOP PHP Klasse/Objekte PHP Konstrukteur PHP vernietiger PHP Toegangswysigers PHP erfenis PHP konstante PHP Abstrakte Klasse PHP-koppelvlakke PHP eienskappe PHP statiese metodes PHP Statiese Eienskappe PHP naamruimtes PHP Iterables

MySQL- databasis

MySQL-databasis MySQL Connect MySQL Skep DB MySQL Skep tabel MySQL Voeg data in MySQL Kry Laaste ID MySQL Voeg veelvuldige in MySQL voorberei MySQL Kies Data MySQL Waar MySQL Bestel deur MySQL verwyder data MySQL-opdateringsdata MySQL-limietdata

PHP XML

PHP XML-ontleders PHP SimpleXML-ontleder PHP SimpleXML - Kry PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX-databasis AJAX XML AJAX Live Search AJAX-peiling

PHP voorbeelde

PHP voorbeelde PHP samesteller PHP vasvra PHP Oefeninge PHP-sertifikaat

PHP- verwysing

PHP Oorsig PHP Skikking PHP-kalender PHP datum PHP gids PHP fout PHP-uitsondering PHP lêerstelsel PHP filter PHP FTP PHP JSON PHP sleutelwoorde PHP Libxml PHP-pos PHP Wiskunde PHP Diverse PHP MySQLi PHP-netwerk PHP-uitsetbeheer PHP RegEx PHP SimpleXML PHP-stroom PHP-string PHP veranderlike hantering PHP XML-ontleder PHP zip PHP Tydsones

PHP -uitsonderings


Wat is 'n uitsondering?

'n Uitsondering is 'n voorwerp wat 'n fout of onverwagte gedrag van 'n PHP-skrip beskryf.

Uitsonderings word gegooi deur baie PHP-funksies en -klasse.

Gebruiker gedefinieerde funksies en klasse kan ook uitsonderings gooi.

Uitsonderings is 'n goeie manier om 'n funksie te stop wanneer dit op data afkom wat dit nie kan gebruik nie.


Gooi 'n uitsondering

Die throwstelling laat 'n gebruikergedefinieerde funksie of metode toe om 'n uitsondering te gooi. Wanneer 'n uitsondering gegooi word, sal die kode wat daarop volg nie uitgevoer word nie.

As 'n uitsondering nie gevang word nie, sal 'n noodlottige fout voorkom met 'n "Ongevang Uitsondering" boodskap.

Kom ons probeer om 'n uitsondering te gooi sonder om dit te vang:

Voorbeeld

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

echo divide(5, 0);
?>

Die resultaat sal iets soos volg lyk:

Fatal error: Uncaught Exception: Division by zero in C:\webfolder\test.php:4
Stack trace: #0 C:\webfolder\test.php(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4

Die probeer...vang Verklaring

Om die fout van die voorbeeld hierbo te vermy, kan ons die try...catchstelling gebruik om uitsonderings op te vang en die proses voort te sit.

Sintaksis

try {
  code that can throw exceptions
} catch(Exception $e) {
  code that runs when an exception is caught
}

Voorbeeld

Wys 'n boodskap wanneer 'n uitsondering gegooi word:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide.";
}
?>

Die vangblok dui aan watter tipe uitsondering gevang moet word en die naam van die veranderlike wat gebruik kan word om toegang tot die uitsondering te verkry. In die voorbeeld hierbo is die tipe uitsondering Exceptionen die veranderlike naam is $e.


Die probeer...vang...uiteindelik Verklaring

Die try...catch...finallystelling kan gebruik word om uitsonderings te vang. Kode in die finallyblok sal altyd loop, ongeag of 'n uitsondering gevang is. Indien finallyteenwoordig is, is die catchblok opsioneel.

Sintaksis

try {
  code that can throw exceptions
} catch(Exception $e) {
  code that runs when an exception is caught
} finally {
  code that always runs regardless of whether an exception was caught
}

Voorbeeld

Wys 'n boodskap wanneer 'n uitsondering gegooi word en dui dan aan dat die proses beëindig is:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide. ";
} finally {
  echo "Process complete.";
}
?>

Voorbeeld

Voer 'n string uit, selfs al is 'n uitsondering nie opgespoor nie:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} finally {
  echo "Process complete.";
}
?>

Die Uitsonderingsvoorwerp

Die Uitsonderingsvoorwerp bevat inligting oor die fout of onverwagte gedrag wat die funksie teëgekom het.

Sintaksis

new Exception(message, code, previous)

Parameterwaardes

Parameter Description
message Optional. A string describing why the exception was thrown
code Optional. An integer that can be used used to easily distinguish this exception from others of the same type
previous Optional. If this exception was thrown in a catch block of another exception, it is recommended to pass that exception into this parameter

Metodes

Wanneer 'n uitsondering gevang word, toon die volgende tabel sommige van die metodes wat gebruik kan word om inligting oor die uitsondering te kry:

Method Description
getMessage() Returns a string describing why the exception was thrown
getPrevious() If this exception was triggered by another one, this method returns the previous exception. If not, then it returns null
getCode() Returns the exception code
getFile() Returns the full path of the file in which the exception was thrown
getLine() Returns the line number of the line of code which threw the exception

Voorbeeld

Voer inligting uit oor 'n uitsondering wat gegooi is:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero", 1);
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $ex) {
  $code = $ex->getCode();
  $message = $ex->getMessage();
  $file = $ex->getFile();
  $line = $ex->getLine();
  echo "Exception thrown in $file on line $line: [Code $code]
  $message";
}
?>

Voltooi uitsonderingsverwysing

Vir 'n volledige verwysing, gaan na ons Volledige PHP-uitsonderingsverwysing .

Die verwysing bevat beskrywings en voorbeelde van alle uitsonderingsmetodes.