When to consider using an exception mechanism
Error codes and exceptions are methods that return execution results in all development languages, and when writing code, the market needs to consider when to use exception handling and when to consider returning an error code.
Error code
1) The error code is more direct, using code number to represent the result of execution, high performance.
2) usually in a simple error expression can be considered to use. For example, returning false indicates that the processing failed and the other data represents the correct processing result.
3) There are a number of processing results that are usually considered in the form of a return array: [' Code ' = ' 001 ', ' msg ' = ' = ' processing failed! '], you can also provide the GetErrorInfo () function, which is used to obtain the error message after processing is complete. This can compensate for the lack of detailed error code return information, but there is still the caller does not necessarily handle the return information, the error code is added, it does not necessarily notify the caller to handle the error message and so on.
Exception handling
1) performance is slightly lower because the program processing needs to enter and leave the stack.
2) The caller does not miss the newly added error message, and the returned error message is more detailed. Usually the successful execution returns the correct result, and the execution failure returns the exception information.
Sum up:
Which way to use the feedback error, also depends on the scene. Personally, the code module with high operating frequency minimizes the use of exception handling, but ensures that the return information is concise. While some failure factors are not exhaustive, consider using exceptions if performance issues do not occur.
PHP Exception handling code
thrownewException("error balabalabala...");
try//If the exception is thrown, this text will not be showncatch(Exception$eecho‘Message: ‘ .$e->getMessage(); }
- Setting the top level exception processor (top Exception Handler)
You can set the top-level exception handling class to handle all exceptions.
set_exception_handler(‘myException‘);
PHP Exception Handling