Example usage for java.lang Exception Exception

List of usage examples for java.lang Exception Exception

Introduction

In this page you can find the example usage for java.lang Exception Exception.

Prototype

public Exception(String message, Throwable cause) 

Source Link

Document

Constructs a new exception with the specified detail message and cause.

Usage

From source file:Sha1.java

public static String sha1(String text) {
    try {/*w  w w  .  j  a va2s .  c  om*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");

        md.update(text.getBytes("iso-8859-1"), 0, text.length());

        return convertToHex(md.digest());
    } catch (Exception e) {
        throw new Exception(format("Problem hashing %s", text), e);
    }
}

From source file:Main.java

public static Object readBeanFromXml(String path) throws Exception {
    FileInputStream fis = null;//from   w w w . java2  s  . c  om
    Object aplicacion = null;
    BufferedInputStream bis = null;
    try {
        fis = new FileInputStream(path);
        bis = new BufferedInputStream(fis);
        XMLDecoder xmlDecoder = new XMLDecoder(bis);
        aplicacion = (Object) xmlDecoder.readObject();

    } catch (FileNotFoundException ex) {
        throw new Exception("File to read not found.", ex);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ex) {
                throw new Exception("File to close FileInputStream after read.", ex);
            }
        }

        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ex) {
                throw new Exception("File to close BufferedInputStream after read.", ex);
            }
        }
    }

    return aplicacion;

}

From source file:Main.java

public static boolean hasValidXmlDeclaration(byte[] doc) throws Exception {
    try {// www  .  j a  v  a  2s  . c o  m
        XMLStreamReader r = xif.createXMLStreamReader(new ByteArrayInputStream(doc), null);
        if (!"UTF-8".equalsIgnoreCase(r.getEncoding()))
            return false;
        if (!"1.0".equals(r.getVersion()))
            return false;

        r.close();
        return true;
    } catch (XMLStreamException e) {
        throw new Exception("Unable to parse xml", e);
    }
}

From source file:Main.java

/**
 * Performs a xpath query on a document and returns the matching nodelist.
 *
 * @param doc/*from   w w  w. j  av  a 2 s.  com*/
 * @param xpathQuery
 * @return nodelist of matches
 * @throws Exception
 */
public static NodeList query(Document doc, String xpathQuery) throws Exception {
    NodeList result = null;
    // prepare XPath
    XPath xpath = XPathFactory.newInstance().newXPath();

    try {
        // query xml
        result = (NodeList) xpath.evaluate(xpathQuery, doc, XPathConstants.NODESET);
    } catch (XPathExpressionException xpx) {
        throw new Exception("Error evaluating XPath: " + xpx.getMessage(), xpx);
    }
    return result;
}

From source file:Main.java

/** Given a parent element, locate double value of a child node.
 *  @param parent Parent element//w  ww  .ja v  a2  s . c o m
 *  @param name Name of child element
 *  @return Value of child element, or empty result
 *  @throws Exception on error parsing the number
 */
public static Optional<Double> getChildDouble(final Element parent, final String name) throws Exception {
    final Element child = getChildElement(parent, name);
    if (child == null)
        return Optional.empty();
    try {
        return Optional.of(Double.valueOf(getString(child)));
    } catch (NumberFormatException ex) {
        throw new Exception("Expected double for <" + name + ">", ex);
    }
}

From source file:Main.java

/** Given a parent element, locate integer value of a child node.
 *  @param parent Parent element//from   w  w w  .ja  va 2 s  .co m
 *  @param name Name of child element
 *  @return Value of child element, or empty result
 *  @throws Exception on error parsing the number
 */
public static Optional<Integer> getChildInteger(final Element parent, final String name) throws Exception {
    final Element child = getChildElement(parent, name);
    if (child == null)
        return Optional.empty();
    try {
        return Optional.of(Integer.valueOf(getString(child)));
    } catch (NumberFormatException ex) {
        throw new Exception("Expected integer for <" + name + ">", ex);
    }
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws Exception {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    //--- Create a new factory instance via JAXP
    try {//from  ww w.j a va  2 s . com
        factory = DocumentBuilderFactory.newInstance();
    } catch (FactoryConfigurationError e) {
        throw new Exception("XmlUtil.parse() : Cannot create DocumentBuilderFactory ", e);
    } catch (Throwable t) {
        throw new Exception("XmlUtil.parse() : Cannot create DocumentBuilderFactory ", t);
    }
    if (factory != null) {
        //--- Create a DOM parser ( instance of a builder )
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new Exception("XmlUtil.parse() : Cannot create DocumentBuilder ", e);
        } catch (Throwable t) {
            throw new Exception("XmlUtil.parse() : Cannot create DocumentBuilder ", t);
        }
    } else {
        throw new Exception("XmlUtil.parse() : DocumentBuilderFactory instance is null");
    }

    if (builder == null) {
        throw new Exception("XmlUtil.parse() : DocumentBuilder instance is null");
    }
    return builder;
}

From source file:Main.java

/**
 * Check if the file provide is PKCS12/*from www.  ja  v a2  s.  c o m*/
 * @param cert certificate to be validated
 * @param pass password to be provided
 * @throws Exception to indicate an invalid certificate
 */
public static void validate(byte[] cert, String pass) throws Exception {

    try {
        KeyStore keyStore = KeyStore.getInstance(ALGORITHM);
        keyStore.load(new ByteArrayInputStream(cert), pass.toCharArray());
    } catch (Exception e) {
        throw new Exception("Certificate is not valid!", e);
    }
}

From source file:Main.java

/**
 * Reads an xml string into XML Document.
 * /*  w ww  . java 2s.co m*/
 * @param xmlStr String containing xml
 * @return xml Document
 * @throws Exception
 */
public static Document readXmlString(String xmlStr) throws Exception {

    Document doc = null;

    try {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false);

        DocumentBuilder builder = domFactory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlStr));
        doc = builder.parse(inStream);
    } catch (ParserConfigurationException e) {
        throw new Exception(e.getMessage(), e);
    } catch (SAXException e) {
        throw new Exception(e.getMessage(), e);
    } catch (IOException e) {
        throw new Exception(e.getMessage(), e);
    } catch (Exception e) {
        throw new Exception(e.getMessage(), e);
    }

    return doc;

}

From source file:Main.java

/**
 * Parse the content of the given file as an XML document and return a new DOM
 * //from   w  ww.  j  a  va 2 s .c  om
 * @param fileName
 * @return xml Document
 * @throws Exception
 */
public static Document readXmlFile(String fileName) throws Exception {

    Document doc = null;

    try {
        File xmlFile = new File(fileName);
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false);

        DocumentBuilder builder = domFactory.newDocumentBuilder();
        doc = builder.parse(xmlFile);

    } catch (ParserConfigurationException e) {
        throw new Exception(e.getMessage(), e);
    } catch (Exception e) {
        throw new Exception(e.getMessage(), e);
    }
    return doc;

}