Discussion on the use method of try{}catch{} in PHP _php example

Source: Internet
Author: User
Tags exception handling php language

The try{}catch{} in PHP is used to handle exceptions. You can collect and display error messages for us. Hope that through the introduction of this article, we can grasp the application of this statement.

There are a lot of grammars in the PHP language that we need to be familiar with, and then we can use them flexibly and write the code programs we need. In this article we will introduce the usage of try{}catch{} in PHP.

<?php  
	try { 
		//...	
	} catch (Exception $e) {
		//...
	}
? >

try{}catch{} in PHP is exception handling.

The code to be executed is placed in a try block, and if one of the statements in the execution of the code has an exception, the program jumps directly into the catch block, where the error message and display are collected by $e.

PHP try{}catch{} statement

To further handle the exception, we need to use the PHP try{}catch{}----include the try statement and at least one of the catch statements. Any code that invokes a method that might throw an exception should use a try statement. Catch statements are used to handle exceptions that may be thrown.

Here's how we handle the exception thrown by Getcommandobject ():

<?php  
	try {  
		$mgr = new Commandmanager ();  
		$cmd = $mgr->getcommandobject ("Realcommand");  
		$cmd->execute ();  
	} catch (Exception $e) {  
		print $e->getmessage ();  
		Exit ();  
>

As you can see, by using the Throw keyword in conjunction with PHP try{}catch{}, we can avoid the error marking the value returned by the "pollution" class method. Because "exception" itself is a type of PHP that is different from any other object, it does not create confusion.

If an exception is thrown, the script in the Try statement stops executing, and then immediately moves to the script in the catch statement.

If the exception is thrown and is not captured, a fatal error is generated.

The above article on the use of PHP try{}catch{} is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.