How to implement exception handling in PHP program

Source: Internet
Author: User
Tags php script

An exception (Exception) is an exception or an event that occurs during the execution of a program that interrupts the operation of the instruction and jumps to the other program module to continue execution. Therefore, exception handling is often used as the control flow of the program. Whether it is a mistake or an exception, the application must be able to handle it in a proper manner and react accordingly, hoping not to lose data or cause the program to crash. Exception handling is used to change the normal process of a script when a specified error occurs, and is a new and important feature in PHP5. Exception handling is an extensible and maintainable mechanism for error handling, and provides a new object-oriented error handling method. This exception handling mechanism is provided early in languages such as Java, C #, and Python, and if you are familiar with exception handling in a particular language, then the exception handling mechanism provided in PHP is not unfamiliar.

Exception handling and programming process control is similar, so you can also use exception processing to achieve an alternative conditional selection structure. Exceptions are unexpected events that occur during a program's operation, and if this event is not handled, the program encounters an exception when it executes and crashes. Handling exceptions requires the following statement to be used in the PHP script:

try{//All code that requires exception handling must be placed in this block of code

...//You can use the throw statement to throw an exception object here
}catch (EX1) {//Use this block of code to catch an exception and process
...//Handle an exception that occurs, or you can throw an exception again
}

The correct handler should include: try– the function that uses the exception should be in the "Try" code block. If no exception is triggered, the code will continue to execute as usual.  However, if an exception is triggered, an exception is thrown. throw– here specifies how to trigger an exception. Each "throw" must correspond to at least one "catch" catch– "Catch" code block to catch an exception and create an object containing the exception information the exception generated in the PHP code can be thrown by the throw statement and caught by a catch statement. The code that requires exception handling must be placed inside a try code block to catch possible exceptions. Each try must have at least one catch corresponding to it, nor can there be a separate catch, and no code can appear between the try and the cache. A simple example of exception handling is as follows:
12345678910 <?phptry{$error=‘Always throw this error‘;throw new Exception($error); //创建一个异常对象,通过throw语句抛出echo ‘Never executed‘; //从这里开始,try代码块内的代码将不会再被执行}catch(Exception $e){echo ‘Caught exception;‘, $e->getMessage(),"\n"; //输出捕获的异常消息}echo ‘Hello World‘; //程序没有崩溃继续向下执行?>

In the above code, if there are some errors in the try code, we can perform an operation that throws an exception. In some programming languages, such as Java, an exception is thrown automatically when an exception occurs. In PHP, exceptions must be thrown manually. The Throw keyword punishes the exception handling mechanism, which is a language structure, not a function, but must be passed an object as a value. In the simplest case, you can implement a built-in exception class, as shown in the code above. If an exception object is thrown in a try statement, the code block will no longer continue to execute down, and jump directly into the catch. And passed to the catch code block an object that can also be understood as an object captured by a catch code block, which is the object that caused the exception to be thrown by the throw statement. You can simply output some of the exceptions in the catch code block, or another version of the task in the try code block to resolve the aversion, and you can also create a new exception in this catch block. Most importantly, after exception handling, the program will not crash and will continue to execute.

>> This article fixed link: http://php.ncong.com/php_course/wrong/yichangshixian.html

>> reprint Please specify: Ntshongwana PHP August 05, 2014 Yuncon PHP Learning tutorial Published

How to implement exception handling in PHP program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.