try-catch Java and php Analysis

Description: For a robust system in terms of exception handling is an essential part for managing exceptions, mainly abnormal capture and processing operations, however, the use of try-catch in php, I personally did not feel in java comfortable, because in php in try-catch, you have to manually throw an exception in order to achieve your desired exception handling effects, and different java, you do not need to manually throw an exception in the try zone, it will automatically catch the exception, and process.

php: try-catch Examples

    1: Create a function may throw an exception

       function testError(){

        $s=2/0;

        throw new Exception ( "Value must be 1 or below"); // here must be manually throw an exception for the job

          return true;

       }

 

2: Capture exception "try-catch" code block

  try {

         testError ();

         After echo 'when a is divided by an integer 0, as no manual throw abnormal, will still be performed here. ';

        }catch(Exception $e) {

          echo 'Error Message: ' .$e->getMessage();

   }

Note: there will be after the implementation of the above test procedures following tips:

After seeing some of the tips above, we, as developers will understand roughly what the error is caused by the abnormal, but as a robust system is concerned, these tips should not be displayed to the user, to provide users see Some friendly message, we really let the user information is visible red box (here is the information of the test output, you can do this other friendly treatment), in addition to information on the red box, others are do not show, we just need the php.ini display_errors = On close it, that display_errors = Off. After closing restart the apache, then visit just about testing procedures, prompt information will be displayed only above the red box.
You must manually throw the job when this is mainly to test the php try-catch, verify the following:
   the abnormal function throw this sentence be commented out, and then run the test, the following prompt appears:    "When an integer is divided by a 0, as there is no manual throw an exception, it still performs here "   abnormal this means that when the program appears in the try-catch is not caught in the process, but the following code to execute the page as usual, this is not what we want ; we just comment out again to throw a later removed, it can normally catch the handle exceptions.


java: try-catch Example:    

public class TestTry{

    public static void testError(){

       int i=3,n=0;

       int s=i/n;

    }

   public static void main(String[] args){

    try{

       TestTry.testError();

       System.out.println("Goto here");

    } Catch (Exception) {

       System.out.println("ErrorMessage:"+e);

    }

   }

}

After executing the above verification process: Error Message: java.lang.ArithmeticException: / by zero

NOTE: It can be seen in the above verification program java try-catch block does not have to manually throw, but will automatically try capturing process area.

The next step will be in-depth look at exception handling mechanism php how to automatic operation.

Reproduced in: https: //my.oschina.net/mapsh/blog/598135

Guess you like

Origin blog.csdn.net/weixin_34281537/article/details/91691105