_php example of exception Handling in YII Framework Tutorial

Source: Internet
Author: User
Tags exception handling php file php error smarty template yii

This article describes the YII framework exception handling. Share to everyone for your reference, specific as follows:

Exceptions are ubiquitous, as programmers, living to create these exceptions and then fix them. The YII framework encapsulates the PHP exception, making it easier to handle exceptions.

Configure methods for handling errors and exceptions using Yii:

You can define Yii_enable_error_handler and Yii_enable_exception_handler as true in the entry file.

The situation that caused the exception

1. Triggering onerror or Onexception events

2. Artificially throwing an exception. For example

throw new Exceptionclass (' Error message ')//The base class of the exception
throw new Chttpexception (404, ' This page does not exist ');//end-User class

Display view of exceptions

When an error is forwarded to the component Cerrorhandler, it chooses the appropriate view to display the error.
Cerrorhandler searches for the appropriate view to display the error message in the following order:

1. Webroot/themes/themename/views/system: In the system directory under the current theme view.
2. Webroot/protected/views/system: In the system directory of the default view of the application.
3. Yii/framework/views: In the Standard view directory provided by Yii.

Use the action to handle caught exception errors.

To Modify a configuration file :

/yii_dev/testwebap/protected/config/main.php
    ' ErrorHandler ' =>array (
//use ' site/error ' action to Display errors
      ' erroraction ' => ' site/error ',
    

Use to specify the action to handle the error. For example, the error action in Site Controller
Actionerror default code is as follows

/**
 * The action to handle external exceptions.
 */Public
function Actionerror ()
{
  if ($error =yii::app ()->errorhandler->error)
  {
    if (Yii::app ()->request->isajaxrequest)
      echo $error [' message '];
    else
      $this->render (' Error ', $error);
  }


In this action, you first get a detailed error message from the Cerrorhandler::error. If the information obtained is not empty, the error view is rendered using the information returned by Cerrorhandler::error. The information returned by Cerrorhandler::error is an array of the following structure:

code: HTTP status code (e.g. 403, 500);
Type: Error types (e.g. chttpexception, PHP error);
message: Error messages;
file: The PHP file name where the error occurred;
Line: The row where the error occurs;
Trace: Wrong call stack information;
Source: The context of the code where the error occurred.

Exception Log

Error messages at an error level are logged when the error occurs. If this error is caused by PHP warning or notice, the message will be recorded in the PHP category, and if the error message is caused by an unhandled exception, then the classification will be exception. Exceptionclassname (for Chttpexception, its statuscode will also be appended to the category name). Developers can use these records to monitor the way in which error messages are handled during application execution.

More about Yii related content readers can view the site topics: "Yii framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design based on the YII framework.

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.