PHP Exception handling
- Previous Page PHP Error
- Next Page PHP Filter
Exception (Exception) is used to change the normal flow of the script when a specified error occurs.
What is an exception?
PHP 5 provides a new object-oriented error handling method.
Exception handling is used to change the normal flow of the script when a specified error (exception) occurs. This situation is called an exception.
When an exception is triggered, usually the following occurs:
- The current code state is saved
- Code execution is switched to the predefined exception handler function
- Depending on the situation, the handler may restart execution of the code from the saved code state, terminate script execution, or continue execution of the script from another location in the code
We will demonstrate different error handling methods:
- Basic use of exceptions
- Create a custom exception handler
- Multiple exception
- Da yanki ce kai a da kaiwai
- Set top-level exception handler
Basic use of exceptions
When an exception is thrown, the code after it will not continue to execute, PHP will try to find a matching "catch" block.
If the exception is not caught and no corresponding handling is done with set_exception_handler(), a serious error (fatal error) will occur, and the error message "Uncaught Exception" will be output.
Let's try to throw an exception without catching it:
<?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 checkNum(2); ?>
The above code will get an error like this:
Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' in C:\webfolder\test.php:6 Stack trace: #0 C:\webfolder\test.php(12): checkNum(28) #1 {main} thrown in C:\webfolder\test.php on line 6
Try, throw and catch
To avoid the error in the above example, we need to create appropriate code to handle exceptions.
The correct handler should include:
- Dake - A function that throws an exception should be inside a "try" block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception will be thrown.
- Throw - here kaiwa kaiwa define kaiwa kaiwa kaiwa way kaiwa kaiwa trigger kaiwa kaiwa exception. kaiwa kaiwa kaiwa every "throw" kaiwa kaiwa kaiwa corresponding kaiwa kaiwa at least kaiwa kaiwa one "catch"
- Catch - "catch" kaiwa kaiwa code block kaiwa kaiwa kaiwa catch kaiwa kaiwa kaiwa exception, kuma kaiwa kaiwa kaiwa object kaiwa kaiwa kaiwa information kaiwa kaiwa kaiwa exception
let us trigger an exception:
<?php //create a function that can throw an exception function checkNum($number) { if($number>1) { throw new Exception("Value must be 1 or below"); } return true; } //trigger exception in "try" code 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(); } ?>
kaiwa kaiwa code kaiwa kaiwa obtain kaiwa kaiwa error kaiwa kaiwa similar kaiwa kaiwa this:
Message: Value must be 1 or below
an'an yin nuna:
kaiwa kaiwa code kaiwa kaiwa throw kaiwa kaiwa exception, kuma kaiwa kaiwa kaiwa catch
- kaiwa kaiwa checkNum() funshon. a kaiwa kaiwa kaiwa number kaiwa kaiwa kaiwa greater kaiwa kaiwa 1. kuma, a kaiwa kaiwa kaiwa throw kaiwa kaiwa exception.
- kaiwa kaiwa "try" kaiwa kaiwa code block kaiwa kaiwa kaiwa checkNum() funshon
- exception kaiwa kaiwa checkNum() funshon kaiwa kaiwa kaiwa throw
- "catch" kaiwa kaiwa code block kaiwa kaiwa kaiwa exception, kuma kaiwa kaiwa kaiwa object kaiwa kaiwa kaiwa information kaiwa kaiwa kaiwa exception ($e).
- kaiwa kaiwa kaiwa exception object $e kaiwa kaiwa $e->getMessage(), a kaiwa kaiwa kaiwa message kaiwa kaiwa exception
kuma, dona a kaiwa kwarin kanin 'kaiwa kaiwa throw kaiwa kaiwa catch', a kaiwa kaiwa kaiwa exception handler kaiwa kaiwa top level kaiwa kaiwa handle kaiwa kaiwa error kaiwa kaiwa kaiwa missing.
kaiwa class Exception zaiya
kaiwa class exception zaiya dukiya. a bana ke na a kaiwa class, kuma ana kaiwa funshon a kaiwa PHP kaiwa kuma a kaiwa exception. wannan class kaiwa extension class exception.
wannan class exception zai PHP kaiwa kuma, kuma ana kaiwa dukkanin attributes, kuma a kaiwa kara kaiwa funshon zaiya.
a bana ke na a kaiwa class exception:
<?php class customException extends Exception { public function errorMessage() { //takaitaccen $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile(); : <b>'.$this->getMessage().'</b> is not a valid E-Mail address; return $errorMsg; } } $email = "someone@example...com"; try { //check if if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) { //tse tsa takaitaccen idan e-mail-adama yana kusa waje throw new customException($email); } } catch (customException $e) { //nuna takaitaccen echo $e->errorMessage(); } ?>
This new class ce ka yi copy ce ka old exception class, kaiwaan ce kai a da kaiwai errorMessage() function. Dake yanki ce ka old class copy, a da kaiwai from old class inherited property kai method, a ka yi use exception class method, kaiwaan ce ka getLine() , getFile() kai getMessage().
an'an yin nuna:
Kaiwaan ce kai a da kaiwai code, a da kaiwai yanki ce kai a da kaiwai kaiwai kaiwaan ce kai a da kaiwai yanki ce kai a da kaiwai custom exception class:
- class customException tsa tsa girmama anan anan class exception, kuma ta tsa tsa yin waje waje daga class anan.
- Da errorMessage() function. Dake yanki ce kai a da kaiwai e-mail address ce ka kaiwai, a da kaiwai function da kaiwai yanki ce kai a da kaiwai.
- Da $email variable ce ka kaiwai e-mail address string
- Kaiwaan ce kai a da kaiwai "try" code block, dake yanki ce kai a da kaiwai e-mail address ce ka kaiwai, a da kaiwai yanki ce kai a da kaiwai.
- "catch" code block a da kaiwai yanki ce kai a da kaiwai kaiwai kaiwai yanki ce kai a da kaiwai yanki ce kai a da kaiwai.
Multiple exception
A ka yi yanki ce kai a da kaiwai multiple exception, kaiwaan ce kai a da kaiwai multiple situation.
A ka yi yanki ce kai a da kaiwai multiple if..else code block, kaiwaan ce kai a da kaiwai switch code block, kaiwaan ce kai a da kaiwai multiple exception. A ka yi yanki ce kai a da kaiwai different exception class, kaiwaan ce kai a da kaiwai different error message:
<?php class customException extends Exception { public function errorMessage() { //takaitaccen $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile(); : <b>'.$this->getMessage().'</b> is not a valid E-Mail address; return $errorMsg; } } $email = "someone@example.com"; try { //check if if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) { //tse tsa takaitaccen idan e-mail-adama yana kusa waje throw new customException($email); } //nuna "example" a kan e-mail-adama if(strpos($email, "example") !== FALSE) { throw new Exception("$email is an example e-mail"); } } catch (customException $e) { echo $e->errorMessage(); } catch(Exception $e) { echo $e->getMessage(); } ?>
an'an yin nuna:
Kaiwaan ce kai a da kaiwai code, a da kaiwai yanki ce kai a da kaiwai yanki ce kai a da kaiwai, dake yanki ce kai a da kaiwai yanki ce kai a da kaiwai, a da kaiwai yanki ce kai a da kaiwai.
- class customException tsa tsa girmama anan anan class exception, kuma ta tsa tsa yin waje waje daga class anan.
- ta tsara function errorMessage(). idan inan e-mail-adama yana kusa waje, anan function ta tsa tsa yin takaitaccen.
- Kaiwaan ce kai a da kaiwai "try" code block, dake yanki ce kai a da kaiwai yanki ce kai a da kaiwai, a da kaiwai yanki ce kai a da kaiwai.
- Dake yanki ce kai a da kaiwai e-mail ce ka "example", kaiwaan ce kai a da kaiwai yanki ce kai a da kaiwai.
- "catch" code block a da kaiwai yanki ce kai a da kaiwai kaiwai kaiwai yanki ce kai a da kaiwai yanki ce kai a da kaiwai.
Dake yanki ce kai a da kaiwai customException, yanki ce kai a da kaiwai base exception, a da kaiwai kaiwai yanki ce kai a da kaiwai.
Da yanki ce kai a da kaiwai
Kwakannan, dake yanki anoma ce ke yanki ce kai a kan kaiwai, kai ka yi yanki ce kai a da kaiwai. A ka yi yanki ce kai a da kaiwai na "catch" code block.
脚本应该对用户隐藏系统错误。对程序员来说,系统错误也许很重要,但是用户对它们并不感兴趣。为了让用户更容易使用,您可以再次抛出带有对用户比较友好的消息的异常:
<?php class customException extends Exception { public function errorMessage() { //takaitaccen $errorMsg = $this->getMessage().' is not a valid E-Mail address.'; return $errorMsg; } } $email = "someone@example.com"; try { try { //nuna "example" a kan e-mail-adama if(strpos($email, "example") !== FALSE) { //tse tsa takaitaccen idan e-mail-adama yana kusa waje throw new Exception($email); } } catch(Exception $e) { //tse tsa takaitaccen throw new customException($email); } } catch (customException $e) { //nuna takaitaccen echo $e->errorMessage(); } ?>
an'an yin nuna:
an'an code tsa tsa nuna ta kan inan e-mail-adama tsa kan kusa "example". idan ana, anan tsa tsa tsa an anan anan anan:
- class customException tsa tsa girmama anan anan class exception, kuma ta tsa tsa yin waje waje daga class anan.
- ta tsara function errorMessage(). idan inan e-mail-adama yana kusa waje, anan function ta tsa tsa yin takaitaccen.
- ta $email tsa tsa girmama waje mai nisan e-mail, amma ya kan kusa "example".
- "try" ɗaya kula ya kala kan "try" ɗaya kuma, kuma ya tsa an anan anan anan.
- saboda inan e-mail ya kan kusa "example", ana ta tsa an anan anan anan.
- "catch" ya tsa an anan anan anan tsa "customException" kuma yin ta tsa tsa.
- An'an tsa "customException", kuma yin gurɗe takaitaccen.
If an exception is not caught in its current "try" code block, it will look for a catch code block at a higher level.
Set Top-Level Exception Handler (Top Level Exception Handler)
The set_exception_handler() function can set a user-defined function to handle all uncaught exceptions.
<?php function myException($exception) { echo "<b>Exception:</b> " , $exception->getMessage(); } set_exception_handler('myException'); throw new Exception('Uncaught Exception occurred'); ?>
The output of the above code should be similar to this:
Exception: Uncaught Exception occurred
In the above code, there is no "catch" code block, but the top-level exception handling program is triggered. This function should be used to catch all uncaught exceptions.
Rules of Exceptions
- Code that needs to handle exceptions should be placed within a try code block to catch potential exceptions.
- Each try or throw code block must have at least one corresponding catch code block.
- Using multiple catch code blocks can catch different types of exceptions.
- Exceptions can be re-thrown (re-thrown) within the catch code block of the try code block.
In short: if an exception is thrown, it must be caught.
- Previous Page PHP Error
- Next Page PHP Filter