Strus2 learning Exception Handling highlights (1), strus2exception

Source: Internet
Author: User

Strus2 learning Exception Handling highlights (1), strus2exception

2015-01-05

A Method written by dom4j used to change the node text reported the following error:

Code:

for(Iterator itr=root.elementIterator();itr.hasNext();){                Element ele=(Element)itr.next();                System.out.println((Element)ele.element("name")==null);                if(ele.element("name").getText().equals(name)){                    ele.element("age").setText(age);                    break;                }            }

Xml:

<? Xml version = "1.0" encoding = "gb2312"?> <Student> <name> Zhang San </name> <age> 20 </age> <name> Li Si </name> <age> 30 </age> </Student>

Analysis:

Let's review his code. First, exception will solve some problems.

Catch (IOException e ){
E. printStackTrace ();
}
Catch (incluentexception e ){
E. printStackTrace ();
}

So that the key to the error cannot be seen later!

The code snippet is used to search for the "name" element under the subnode loop of "student". In this case, it becomes "name" under "name", so the node cannot be found. If the exception is normal, the reminder element cannot be found.

Remove the for loop and the code runs normally to get the desired result!

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.