PHP Try-catch statement usage tips, try-catch usage tips_PHP tutorial

WBOY
Release: 2016-07-12 08:58:18
Original
1043 people have browsed it

PHP Try-catch statement usage tips, try-catch usage tips

PHP Try-catch statement
In order to further handle exceptions, we need to use try-catch statements-including Try statements and at least one catch statement. Any code that calls a method that may throw an exception should use a try statement. The Catch statement is used to handle exceptions that may be thrown. The following shows how we handle exceptions thrown by getCommandObject():

<&#63;php 
try { 
  $mgr = new CommandManager(); 
  $cmd = $mgr->getCommandObject("realcommand"); 
  $cmd->execute(); 
} catch (Exception $e) { 
  print $e->getMessage(); 
  exit(); 
} 
&#63;>
Copy after login

As you can see, by using the throw keyword in conjunction with the try-catch statement, we can avoid "polluting" the value returned by the class method with error tags. Because "exception" itself is a PHP built-in type that is different from any other object, there will be no confusion.

If an exception is thrown, the script in the try statement will stop executing, and then immediately switch to executing the script in the catch statement.

If an exception is thrown but not caught, a fatal error will be generated.

Articles you may be interested in:

  • php Try Catch exception test
  • Detailed explanation of try catch exception examples in php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1104326.htmlTechArticlePHP Try-catch statement usage tips, try-catch usage tips PHP Try-catch statement In order to further handle exceptions, we You need to use the try-catch statement----including the Try statement and at least one ca...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!