当前位置: 首页>>代码示例>>Java>>正文


Java XPathException.initCause方法代码示例

本文整理汇总了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.");
    }

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:59,代码来源:XPathExceptionInitCause.java


注:本文中的javax.xml.xpath.XPathException.initCause方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。