Flash as2 error

Source: Internet
Author: User
Tags json object object throw exception try catch

A few days ago, I was debugging a program for AS2 to communicate with the background, so I couldn't see the normal data display. Then I went through trace and finally found the problem.
We used a JSON class of AS2. If the format is not in json format, an exception is thrown because our data format is correct or there is no data, otherwise, no format error occurs, and no try catch occurs.
In this test, there was no data in the element, and the json string was "" (null string). Then an exception was thrown during parsing. If I didn't capture it, I ended the program.
See how to throw an exception in JSON of AS2:

 

The code is as follows: Copy code
Function error (m ){
Throw {
Name: 'jsonerror ',
Message: m,
At: at-1,
Text: text
};

} An Object instead of an Error instance is directly thrown here. However, the exception message of AS2 is different from that of as3.
AS3:
An error message is displayed in the browser with debug flashplayer.
An error message similar to the following is displayed in the output panel of IDE.

The code is as follows: Copy code
Error: this is an error!
At _ fla: MainTimeline/frame1 () AS2:

No error is prompted in the browser with debug flashplayer installed.
In the output panel of IDE, only the thrown objects or types of strings are output.

This is an error! OK. After reading the two differences, let's look at the method in which the JSON of AS2 throws an exception. It only throws an Object.
Then, the Object object is converted to a String that defaults to [Object.
When I saw this string output, I first thought about where to directly output the object. I didn't even think it would be an exception thrown by JSON.
So the problem is hard to be found, and I finally found the cause in a trace line.

Now, with the above experience, I can customize some rules to make it easier to discover problems.
When a throw exception occurs, the system must throw an Error or an Error subclass instance.
Because Error has the following advantages:

The Error itself has the toString () method, so we will not worry about the fuzzy [object Object.
Errors in AS3 include the getStackTrace () method to track where errors occur, although they can only be used in the debug version.
Normally, when an exception is caught, we only judge that Error is similar to catch (err: Error). However, if we are not a subclass of throw Error or Error, this exception cannot be caught.

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.