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(Throwable cause) 

Source Link

Document

Constructs a new exception with the specified cause and a detail message of (cause==null ?

Usage

From source file:Factorial.java

/**
* Returns the Factorial of a number that
* is passed into the method through command
* line arguments.//from   ww  w  .j a  v  a 2 s .  c  om
*
* @param a The counting variable and initial condition for the Factorial.
* @param factorial The running value of the Factorial.
* @throws NegativeNumberException if the input given is negative.
* @returns the calculated Factorial.
*/
public static double calcFactorial(int a) throws Exception {
    double factorial = a;

    // throw an exception if the input is negative
    if (a < 0) {
        throw new Exception("The user has entered a " + "negative number.");
    }

    for (int i = (a - 1); i > 1; i--)
        factorial *= i;

    return factorial;
}

From source file:Main.java

/**
 * Convert XML {@link Document} to its string representation.
 *
 * @param document for conversion/*  w w w . j a  v  a2 s .  c  o m*/
 * @return - string representation of XML {@link Document}
 * @throws Exception - if {@link DocumentBuilder} is not initialized
 */
public static String xmlToString(Document document) throws Exception {
    if (transformer == null) {
        throw new Exception("Transformer is null");
    }
    Source xmlSource = new DOMSource(document);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    transformer.transform(xmlSource, result);
    return stringWriter.toString();
}

From source file:Main.java

private static Node getSingleNodeElementByTagName(String tagName, int indexZeroBased) throws Exception {
    NodeList list = getNodeList(tagName);
    Node node = null;//from www . j  a v  a2  s. com

    if (list.getLength() > 0 && list.item(indexZeroBased).getNodeType() == Node.ELEMENT_NODE) {
        node = list.item(indexZeroBased);
    } else {
        throw new Exception("Xpath Query did not result in any Node elements. Check your Xpath expression");
    }

    return node;
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws Exception {
    try {// w w w.j av  a  2 s. c o  m
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new Exception(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}

From source file:Main.java

/**
 * Convert item reference number to string of length according to partition
 *
 * @param itemRefNo irn//from ww  w.  ja va  2  s  .c  o m
 * @param partition p
 * @return Item reference number string
 * @throws Exception
 */
private static String getItemRefNoString(int itemRefNo, byte partition) throws Exception {
    String result = Integer.toString(itemRefNo);
    int length;
    switch (partition) {
    case 0:
        length = 1;
        break;
    case 1:
        length = 2;
        break;
    case 2:
        length = 3;
        break;
    case 3:
        length = 4;
        break;
    case 4:
        length = 5;
        break;
    case 5:
        length = 6;
        break;
    case 6:
        length = 7;
        break;
    default:
        throw new Exception("Unknown partition: " + partition);
    }
    while (result.length() < length)
        result = "0" + result;
    return result;
}

From source file:Main.java

/**
 * Convert company prefix to string of length according to partition
 *
 * @param companyPrefix cp// w w  w  .  j a va 2  s  . c  o  m
 * @param partition     p
 * @return Company prefix string
 * @throws Exception
 */
private static String getCompanyPrefixString(long companyPrefix, byte partition) throws Exception {
    String result = Long.toString(companyPrefix);
    int length;
    switch (partition) {
    case 0:
        length = 12;
        break;
    case 1:
        length = 11;
        break;
    case 2:
        length = 10;
        break;
    case 3:
        length = 9;
        break;
    case 4:
        length = 8;
        break;
    case 5:
        length = 7;
        break;
    case 6:
        length = 6;
        break;
    default:
        throw new Exception("Unknown partition: " + partition);
    }
    while (result.length() < length)
        result = "0" + result;
    return result;
}

From source file:Main.java

public static void updateNodeToXml(String nodeXpathStr, String xmlFilePath, String value,
        Map<String, String> attr) throws Exception {
    Document doc = null;//from  w  w  w .j av a  2  s. c  om
    if (xmlFilePath == null || nodeXpathStr == null)
        throw new Exception("some parameters can not be null!");
    doc = dombuilder.parse(new File(xmlFilePath));
    Node pNode = (Node) xpath.compile(nodeXpathStr).evaluate(doc, XPathConstants.NODE);
    if (pNode == null)
        throw new Exception("can not find the node specified in nodeXpathStr!");
    ;
    pNode.setTextContent(value);

    if (attr != null && !attr.isEmpty()) {
        for (String key : attr.keySet())
            ((Element) pNode).setAttribute(key, attr.get(key));
    }

    writeToXmlFile(doc, xmlFilePath);
}

From source file:Main.java

public static byte[] encryptMessage(String key, String input) throws Exception {
    if (checkNull(input)) {
        return null;
    }/*from  w  ww  .  j a v  a  2s . c om*/
    try {
        return doFinal(key, Cipher.ENCRYPT_MODE, input.getBytes());
    } catch (Exception e) {
        throw new Exception("201");
    }
}

From source file:Main.java

public static String getFirstValueFromXPath(Document parent, String xpath) throws Exception {
    try {//  w  ww.  j  a  v  a 2 s  .c  om
        XPath xPath = XPathFactory.newInstance().newXPath();
        return (String) xPath.compile(xpath).evaluate(parent, XPathConstants.STRING);
    } catch (XPathExpressionException xpee) {
        throw new Exception(xpee.getMessage());
    }
}

From source file:Main.java

public static byte[] decryptMessage(String key, byte[] input) throws Exception {
    if ((input == null) || (input.length <= 0)) {
        return null;
    }//from  ww w  . j  a  v a2 s . c o m
    try {
        return doFinal(key, Cipher.DECRYPT_MODE, input);
    } catch (Exception e) {
        throw new Exception("201");
    }
}