Murphy's Law and IndexOutOfBoundsException (array out-of-bounds exception ),

Source: Internet
Author: User

Murphy's Law and IndexOutOfBoundsException (array out-of-bounds exception ),

Another problem was reported during maintenance today. An array out-of-bounds exception occurred when querying the exam:

1 2017-02-28 10:45:24,827[ERROR] HttpException[10.32.111.7:60446:2D07867BE98F56D5EFFA1B1A597776AC]:/WAserver/HS851020 [com.hundsun.hsacct.core.httpresult.HandlerExceptionResolver.printHttpLog(HandlerExceptionResolver.java:156)] 2 java.lang.ArrayIndexOutOfBoundsException: 13     at com.hundsun.hsacct.action.paper.QryEligPaperAction.qryEligPaper(QryEligPaperAction.java:69)4     at sun.reflect.GeneratedMethodAccessor2434.invoke(Unknown Source)5     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)6     at java.lang.reflect.Method.invoke(Method.java:606)

After reading the code written by the developer, there are two lines that use commas as the separator to split the string. Basically, the cause of the problem is known. At this time, I was quietly reminded of Murphy's Law: as long as there is a possibility of an error in anything, it will definitely make a mistake in the end. It's just because the time has not arrived. The theory of the master is amazing.

String answer_content = map. get ("answer_content"); if (StringUtils. isNotBlank (answer_content) {// delimiter, 'string [] stepOne = answer_content.split (","); Map <String, String> answer = new HashMap <String, string> (); for (int I = 0; I <stepOne. length; I ++) {// second split, separator ', 'string [] stepTwo = stepOne [I]. split (","); // Add if (answer. get (stepTwo [0]) = null) {answer. put (stepTwo [0], stepTwo [1]);} else {answer. put (stepTwo [0], stepTwo [1] + "," + answer. get (stepTwo [0]);} paper. put (Fields. ANSWER_CONTENT, answer);} else {paper. put (Fields. ANSWER_CONTENT ,"");}

In fact, when I reviewed the code, I had no intention to give it a look. I vaguely felt that there was a pitfall in the code, but there was no problem in the test. In addition, the interface's business logic was simple, so I didn't take it seriously, as a result, production was almost finished, and the problem was leaked.

After the problem arises, in addition to the lack of experience of developers, there is more self-blame for themselves. After all, the code written by developers has passed their own review, why is it easy to let the problem go?

Here is an in-depth analysis of the problem:

IndexOutOfBoundsException (array out-of-bounds exception) is one of the most common exceptions in Java code, and NullPointerException (NULL pointer exception ). Cause: it is the most common reason to split uncontrollable strings and access the data. What is "uncontrollable String"? It is a String other than our own program. What is "controllable String"? It is a String in our system, such as a constant defined in our code and a fixed configuration file used by our system.

Similar to StringIndexOutOfBoundsException, this exception is thrown when you perform the substring operation on a character string with insufficient length. The reason for this exception is that the uncontrollable String is used.

As an experienced programmer, be cautious when you see Code such as String. split () and String. substring. It is very likely that a time bomb has been planted here, and there is no problem for the time being, just because the time has not arrived.

Better Processing Method: when we have to perform split () and substring () operations on uncontrollable strings, we should check them. If the string does not conform to the format requirements, we should take the initiative to throw an exception, so that system maintenance personnel can immediately know that the current system does not meet the operating conditions and need to repair, so as to minimize the failure loss.

 

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.