本文整理汇总了Java中javax.xml.xpath.XPathException.initCause方法的典型用法代码示例。如果您正苦于以下问题:Java XPathException.initCause方法的具体用法?Java XPathException.initCause怎么用?Java XPathException.initCause使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.xpath.XPathException
的用法示例。
在下文中一共展示了XPathException.initCause方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import javax.xml.xpath.XPathException; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Throwable cause = new Throwable("message 1");
XPathException xpathexcep = new XPathException("message 2");
//Test XPE initCause() method
xpathexcep.initCause(cause);
System.out.println("getCause() result: '" + xpathexcep.getCause()
+ "' Cause itself: '" + cause + "'");
if (!xpathexcep.getCause().toString().equals(cause.toString())) {
throw new Exception("Incorrect cause is set by initCause()");
}
//Test serialization/deserialization of initialized XPE
byte [] xpeserial;
XPathException xpedeser;
xpeserial = pickleXPE(xpathexcep);
xpedeser = unpickleXPE(xpeserial);
System.out.println("Serialized XPE: message='" + xpathexcep.getMessage()
+ "' cause='" + xpathexcep.getCause().toString() + "'");
System.out.println("Deserialized XPE: message='" + xpedeser.getMessage()
+ "' cause='" + xpedeser.getCause().toString()+"'");
if(xpedeser.getCause() == null ||
!xpedeser.getCause().toString().equals(cause.toString()) ||
!xpedeser.getMessage().toString().equals("message 2") )
throw new Exception("XPathException incorrectly serialized/deserialized");
//Test serialization/deserialization of uninitialized cause in XPE
XPathException xpeuninit = new XPathException("uninitialized cause");
xpeserial = pickleXPE(xpeuninit);
xpedeser = unpickleXPE(xpeserial);
System.out.println("Serialized XPE: message='" + xpeuninit.getMessage()
+ "' cause='" + xpeuninit.getCause()+"'");
System.out.println("Deserialized XPE: message='" + xpedeser.getMessage()
+ "' cause='" + xpedeser.getCause()+"'");
if(xpedeser.getCause() != null ||
!xpedeser.getMessage().toString().equals("uninitialized cause") )
throw new Exception("XPathException incorrectly serialized/deserialized");
//Test deserialization of normal XPathException serialized by JDK7
XPathException xpejdk7 = unpickleXPE(NORMALJDK7SER);
if(xpejdk7 == null || xpejdk7.getCause() == null ||
!xpejdk7.getMessage().equals("message 2") ||
!xpejdk7.getCause().getMessage().equals("message 1"))
throw new Exception("XpathException serialized by JDK7 was "
+ "incorrectly deserialized.");
//Test deserialization of XPathException with two causes from JDK7.
//The serialization are done for the following XPathException object:
// new XPathException(new Exception()).initCause(null)
try {
xpejdk7 = unpickleXPE(TWOCAUSES);
throw new Exception("Expected InvalidClassException but it wasn't"
+ " observed");
} catch(InvalidClassException e) {
System.out.println("InvalidClassException caught as expected.");
}
}