"Unreachable code"是什么错误?

Mario_cn 2007-03-28 12:58:53
我在使用Eclipse写Java代码时报了这个错误,请明白人告知一下这个错误该如何解释?谢谢

以下是具体的程序代码:
package domprinter;

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class DOMPrinter
{
/*
* 输出节点的名字和节点的值
*/
public static void printNodeInfo(Node node)
{
System.out.println(node.getNodeName() + " : " + node.getNodeValue());
}
/*
* 采用递归调用,输出指定节点下的所有后代节点
*/
public static void printNode(Node node)
{
short nodeType = node.getNodeType();
switch(nodeType)
{
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println("*****PI start*****");
printNodeInfo(node);
System.out.println("*****PI end*****");
break;
case Node.ELEMENT_NODE:
System.out.println("*****ELEMENT start*****");
printNodeInfo(node);
System.out.println("*****ELEMENT end******");
break;

NamedNodeMap attrs = node.getAttributes();
int attrNum = attrs.getLength();
for(int i=0;i<attrNum;i++)
{
Node attr = attrs.item(i);
System.out.println("*****Attribute start*****");
printNodeInfo(attr);
System.out.println("*****Attribute end*****");
}
break;

case Node.TEXT_NODE:
System.out.println("*****Text start*****");
printNodeInfo(node);
System.out.println("*****Text end*****");
break;
default:
break;
}

Node child = node.getFirstChild();
while(child != null)
{
printNodeInfo(child);
child = child.getNextSibling();
}
}
public static void main(String[] args)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("students.xml"));
printNode(doc);
}
catch(ParserConfigurationException e){e.printStackTrace();}
catch(SAXException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
}

students.xml:

<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xml" href="students.xml"?>
<!DOCTYPE students [
<!ELEMENT students (student*)>
<!ELEMENT student (name,age)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>

<students>
<student sn="01">
<name>张三 </name>
<age>18 </age>
</student>

<student sn="02">
<name>李四 </name>
<age>20 </age>
</student>
</students>
...全文
2578 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mario_cn 2007-04-03
  • 打赏
  • 举报
回复
谢谢,说老实话,这段代码是抄书上的,可能印刷就出现错误了,我该怎么把分加给你呢?
pancras 2007-03-28
  • 打赏
  • 举报
回复
NamedNodeMap attrs = node.getAttributes();
int attrNum = attrs.getLength();
for(int i=0;i<attrNum;i++)
{
Node attr = attrs.item(i);
System.out.println("*****Attribute start*****");
printNodeInfo(attr);
System.out.println("*****Attribute end*****");
}
break;
上边这段没有写在case之后,所以就无法执行,于是就报unreachable code...
Mario_cn 2007-03-28
  • 打赏
  • 举报
回复
报错语句为:NamedNodeMap attrs = node.getAttributes();
提示错误的波浪线在"attrs"下

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧