frankx: Rückgabewerte und Fehlerbehandlung allgemein bzw. am Beispiel

Beitrag lesen

Hellihello Daniel,

Die PHP-API arbeitet wahrscheinlich in solchen Fällen oft mit Rückgabewerten, sie ist aber auch nicht gerade ein Beispiel für gutes API-Design.

http://www.w3schools.com/php/php_exception.asp

darin:

Proper exception code should include:

1. Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown"
   2. Throw - This is how you trigger an exception. Each "throw" must have at least one "catch"
   3. Catch - A "catch" block retrieves an exception and creates an object containing the exception information

  
  
<?php  
//create function with an exception  
function checkNum($number)  
 {  
 if($number>1)  
  {  
  throw new Exception("Value must be 1 or below");  
  }  
 return true;  
 }  
  
//trigger exception in a "try" block  
try  
 {  
 checkNum(2);  
 //If the exception is thrown, this text will not be shown  
 echo 'If you see this, the number is 1 or below';  
 }  
  
//catch exception  
catch(Exception $e)  
 {  
 echo 'Message: ' .$e->getMessage();  
 }  
?>  

Dank und Gruß,

frankx

--
tryin to multitain  - Globus = Planet != Welt