नीचे मैं एक उदाहरण आप कैसे (अपनी स्थिति के लिए गैर विशिष्ट) सामान्य रूप में अपवाद का उपयोग करें और कुछ है कि आप के लिए और अधिक विशिष्ट है (अब भी अपवाद का प्रयोग करके) नीचे आगे बढ़ाने के लिए पता चलता है कि लिखा है। ये पहले दो उदाहरण एक समय में 1 त्रुटि को संभालेगा। मैंने प्रदान किया तीसरा उदाहरण कई त्रुटियों और अपवादों को संभालने का एक उदाहरण देता है।
स्पष्टीकरण के अधिकांश कोड की टिप्पणी में है, इसलिए अच्छी तरह से यह माध्यम से देखने के लिए :)
जनरल अपवाद संचालन सुनिश्चित हो
<?php
//Define some variables to work with
$var = false;
$var2 = false;
try { //Outer try
echo 'Do something here!<br />';
try { //Inner try
if($var !== true) { //Fail
throw new Exception('$var is not true',123); //Exception is thrown (caught 2 lines down)
}
} catch (Exception $e) { //Exception caught here
echo 'InnerError# '.$e->getCode().': '.$e->getMessage().'<br />'; //Exception handled (in this case printed to screen)
}
//Code is continuing here even after the exception was thrown
echo 'Do something else here!<br />';
if($var2 !== true) { //Fail
throw new Exception('$var2 is not true', 456); //Exception is thrown (caught 6 lines down)
}
//Code fails to run as the exception above has been thrown and jumps straight into the below catch
echo 'Do the third thing here!<br />';
} catch (Exception $e) { //Exception caught here
echo 'Error # '.$e->getCode().': '.$e->getMessage().' on line '.$e->getLine().' in '.$e->getFile().'<br />'; //Exception handled (in this case printed to screen)
}
//Code is continuting here even after both of the exceptions
echo 'Do even more stuff here!<br />';
?>
स्टैंडर्ड अपवाद वर्ग निर्माता:
public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]])
कस्टम अपवाद
अब, अपने उदाहरण से संबंधित करते, आप कुछ इन पंक्तियों के साथ कर सकता है:
<?php
class customException extends Exception { //Create a custom exception handler that allows you to pass more arguments in the constructor
public function __construct($errorString, $errorNumber, $errorFile, $errorLine) {
$this->message = $errorString; //Using the Exception class to store our information
$this->code = $errorNumber;
$this->file = $errorFile;
$this->line = $errorLine;
}
}
function err2Exception($errNo, $errStr, $errFile, $errLine) { //This function converts the error into an exception
throw new customException($errStr, $errNo, $errFile, $errLine); //Throw the customException
}
set_error_handler('err2Exception'); //Set the error handler to the above function
try {
assert(1==2); //This fails, calls the function err2Exception with the correct arguments, throws the error and is caught below
} catch (Exception $e) { //Error caught as an Exception here
//Echo out the details (or log them, or whatever you want to do with them)
echo 'Error String: '.$e->getMessage().'<br />';
echo 'Error Number: '.$e->getCode().'<br />';
echo 'File containing error: '.$e->getFile().'<br />';
echo 'Line with error: '.$e->getLine().'<br />';
}
?>
http://php.net/manual/en/function.set-error-handler.php
उपरोक्त कोड का आउटपुट:
Error String: assert(): Assertion failed
Error Number: 2
File containing error: 18
Line with error: /var/www/test2.php
आप कस्टम त्रुटि हैंडलिंग के इस दूसरे उदाहरण के साथ पहली बार कोड उदाहरण में घोंसले try
/catch
बयानों की अवधारणाओं को लागू कर सकते हैं।
हैंडलिंग अनेक त्रुटियां/अपवाद
<?php
class errorLogger { //create an errorLogger class
private $errors; //Stores all errors
public function addError($errCode, $errMsg, $errFile = null, $errLine = null) { //Manually add an error
$this->errors[] = array(//Add to the error list
'code' => $errCode,
'message' => $errMsg,
'file' => $errFile,
'line' => $errLine
);
}
public function addException($exception) { //Add an exception to the error list
$this->errors[] = array(//Add to the error list
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine()
);
}
public function getErrors() { //Return all of the errors
return $this->errors;
}
public function numErrors() { //Return the number of errors
return count($this->errors);
}
}
$el = new errorLogger(); //New errorLogger
set_error_handler(array($el, 'addError')); //Set the default error handler as our errorLoggers addError method
set_exception_handler(array($el, 'addException')); //Set the default exception handler as our errorLoggers addException method
if(!is_numeric('a')) //Will fail
$el->addError('Invalid number', 1); //Adds a new error
if(($name = 'Dave') !== 'Fred') //Will fail
$el->addError('Invalid name ('.$name.')', 2, 'test.php', 40); //Adds another error
assert(1==2); //Something random that fails (non fatal) also adds to the errorLogger
try {
if('Cats' !== 'Dogs') //Will fail
throw new Exception('Cats are not Dogs', 14); //Throws an exception
} catch (Exception $ex) { //Exception caught
$el->addException($ex); //Adds exception to the errorLogger
}
trigger_error('Big bad wolf blew the house down!'); //Manually trigger an error
//throw new Exception('Random exception', 123); //Throw an exception that isn't caught by any try/catch statement
//(this is also added to the errorLogger, but any code under this is not run if it is uncommented as it isn't in a try/catch block)
//Prints out some
echo '<pre>'.PHP_EOL;
echo 'There are '.$el->numErrors().' errors:'.PHP_EOL; //Get the number of errors
print_r($el->getErrors());
echo '</pre>'.PHP_EOL;
?>
जाहिर है आप बदल सकते हैं और विशेष रूप से आपकी आवश्यकताओं के अनुरूप errorLogger
वर्ग अनुकूलित कर सकते हैं।
उपरोक्त कोड का आउटपुट:
There are 5 errors:
Array (
[0] => Array
(
[code] => Invalid number
[message] => 1
[file] =>
[line] =>
)
[1] => Array
(
[code] => Invalid name (Dave)
[message] => 2
[file] => test.php
[line] => 10
)
[2] => Array
(
[code] => 2
[message] => assert(): Assertion failed
[file] => /var/www/test.php
[line] => 42
)
[3] => Array
(
[code] => 14
[message] => Cats are not Dogs
[file] => /var/www/test.php
[line] => 46
)
[4] => Array
(
[code] => 1024
[message] => Big bad wolf blew the house down!
[file] => /var/www/test.php
[line] => 51
)
)
ऊपर कोड आप कर सकते हैं:
- फेंक अपवाद और उन्हें
errorLogger
- में जोड़ने के लिए यादृच्छिक कार्यों से किसी भी बिना क्रिया स्थितियों को संभाल कि आमतौर पर करते हैं त्रुटियों को प्रदर्शित करने के कारण
- अपनी खुद की त्रुटियां मैन्युअल रूप से जोड़ें
- उत्प्रेरक त्रुटियों (http://uk3.php.net/trigger_error)
तो आपको प्रदर्शन/प्रवेश कर सकते हैं/जो कुछ भी बाद में किसी समय त्रुटियों के सभी।
एनबी: उपरोक्त कोड के सभी नकल की और सीधे चिपकाए आप
मुझे इस पुस्तकालय को बहुत पसंद है, मुझे लगता है कि मैं यह देखता हूं कि यह अधिक विस्तार से कैसे काम करता है और अपनी सुविधाओं को अपने स्वयं के ढांचे में दोहराने का प्रयास करता है। धन्यवाद! –
* प्रमाणीकरण * शानदार है, मैंने देखा सबसे अच्छा है। आप के लिए यश! –
+1। आखिरकार मैंने एक त्वरित परियोजना पर 'सम्मान \ सत्यापन' की कोशिश की। बहुत उपयोगी पुस्तकालय! –