我想抛出一个异常,以下是我的异常:
[ fatal error ] Test.xml:18:23:元素类型“值”必须由匹配的结束标记 </value>
终止.
现在我想单独取值 18。
我尝试使用以下代码:
System.out.println(java.util.Arrays.toString(e.getMessage().split(":")));
但正在打印:
[The element type "value" must be terminated by the matching end-tag "</value>".].
如何单独获取值 18?
当我尝试使用以下代码时:
System.out.println(e.getMessage().toString());
,它只是打印 "The element type "value" must be terminated by the matching end-tag "</value>"."
最佳答案
如果你只想打印 18
的值,那么类似这样的事情可能会有所帮助:
String str = "[Fatal Error] Test.xml:18:23: The element type \"value\" must be terminated by the matching end-tag ";
System.out.println(str.split(":")[1]);
产量:18
编辑:第二个想法,e.getMessage()
不会产生像这样的字符串:[Fatal Error] Test.xml:18:23: The element type "value" must be terminated by the matching end-tag </value>
。该字符串似乎是某些日志记录工具的产品,因此您必须从文件或类似的内容中读取它才能获取 18
的值。显示。
关于java - split 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12506361/