How to use Try_catch in YII2-WX

Source: Internet
Author: User
This article mainly introduces about how to use Try_catch in yii2-wx, has a certain reference value, now share to everyone, the need for friends can refer to

As for why to use Try...catch ... Structure I don't want to say much, this post says how to use it in YII2-WX.

So how many exception do we need to focus on in YII2-WX? As you know, YII2-WX is a library of communication classes, and basically every method has a server-initiated operation, where we use the official extension yii2-httpclient of Yii in the simulated HTTP client.

So when we use YII2-WX, the first step is to catch the client exception, the following code

try {    $qrcodeFile = $qrcode->strtemp ($code);} catch (\yii\httpclient\exception $e) {    //Todo}

This is the communication here we are going to catch the yiihttpclientexception exception.

Next talk about Yii2-wx's deconstruction, as a single-entry extension, the application class is very important, and this class has an exception thrown, such as when you use driver to drive an interface helper object, the interface name is not correct, This throw uses YII2-WX built-in exception, so the correct way to use driver is as follows

try {    $app = new Application ([' conf ' = = $conf [' MP ']];        $qrcode = $app->driver ("Mp.qrcode");} catch (\abei2017\wx\core\exception $e) {    //Todo}

It said the whole box, and then said the specific method, such as when we use $qrcode->strtemp ($code), the communication is not a problem, but to obtain the QR code results problems, and now these helper methods will throw a Abei2017wxcoreexception exception, you can get information in $e->getmessage ().

Finally, summarize the complete YII2-WX code writing structure.

try {    $app = new Application ([' conf ' = = $conf [' MP ']];    $qrcode = $app->driver ("Mp.qrcode");    $code = Yii:: $app->security->generaterandomstring ();    $qrcodeFile = $qrcode->strtemp ($code);    } catch (\abei2017\wx\core\exception $e) {    //Todo}catch (\yii\httpclient\exception $e) {    //Todo}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.