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.