Share the "first issue" summary weekly

Source: Internet
Author: User
Tags json try catch

Preface: Not long ago, our eldest brother whim, feel should and the neighboring company to share activities, to promote the rapid growth of our soldiers, so, there is this activity, but "weekly share" the name, it is my personal. Hey, it feels good. Well, the purpose of recording blog, there is a place to review this knowledge, but also want to give me advice or advice, to correct my mistakes. Here's my summary:

1, illegal objects: This is the neighbor company boss proposed, the cause is in looking at their company staff code when found, in the acceptance of third party data, not the validity of the data test, this is what we often say of course, think it should be so. However, when exceptional conditions occur, it can cause our program to crash. Since we don't have code, we don't write code.

2, JSON data processing: When interacting with the server, we all know that the most commonly used function of the JSON function is: json.getstring ("key"); Yes, this is also the method I have been using, json.getxxx ("key"); This is going to throw an exception, that is, the actual code should be like this:

try {
	json.getstring ("key");
} catch (Jsonexception e) {
}

Well, it seems right, the program is very robust, but the actual project is not so simple, in fact, it should be:

The old version of the program
jsonobject JSON = new Jsonobject ();
try {Order
	= New Order ();
	Order.setid (Json.getint ("id"));
	Order.setprice (Json.getint ("price"));
	Order.setctime (Json.getlong ("CTime"));
} catch (Jsonexception e) {
}
If one day, the server said, well, need to add a field utime, developers smile, simple:

The old version of the program
jsonobject JSON = new Jsonobject ();
try {Order
	= New Order ();
	Order.setid (Json.getint ("id"));
	Order.setprice (Json.getint ("price"));
	Order.setctime (Json.getlong ("CTime"));
	Order.setutime (Json.getlong ("Utime"));
} catch (Jsonexception e) {
}
Well, yes, but if it takes a little time for the server to take effect, then, in fact, the weight of the structure does not utime this field, then the problem comes, you might say, I caught it, the program is fine. In fact, after throwing an exception, this order is empty, which is a scene, you refresh, but the list is empty, at this time, I learned a word: defensive programming, we should not envisage the best results, but should be the worst results to plan. To get back to the problem, we can do this for the key fields in the results, but for non-essential fields, we'll use another function. Json.optstring ("Key", "fallback") is not in instant JSON, and no exception is thrown.

3, Shutdown resources: The application and release of resources must be in the right, that is, the application of resources, to release, but for Java may be some of the release is the JVM to do, rather than we, but for the inheritance of autocloseable this class, then, generally we need to solve their own.

4. Exception capture: Try catch we often use, but not all people use the right, there are try to have catch,catch what is inside. If it is only a e.printstacktrace (), then it is equal to that you have caught the exception without processing. The exception is also a program branch, we should also do the appropriate operation to be this branch is complete.

Well, this is what's on the record, and will be updated regularly, including Android-related, Java-related content.


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.