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:Main.java

/**
 * @param child/* w  w  w  . j  a v  a 2 s  .  c o m*/
 * @return the single child element or null
 * @throws Exception if the child is present multiple times
 */
public static Element getChild(Element element, String child) throws Exception {
    NodeList nodes = element.getChildNodes();
    Element ret = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (ret != null) {
                throw new Exception("Child element '" + child + "' present multiple times");
            } else {
                ret = (Element) childNode;
            }
        }
    }
    if (ret == null) {
        return null;
    } else {
        return ret;
    }
}

From source file:Main.java

public static String SimpleHttp(String urlStr) throws Exception {

    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    int resCode = conn.getResponseCode();
    InputStream input = null;/*from  w  w w. j a va2 s  . co  m*/
    String result = null;
    if (resCode == 200) {
        input = conn.getInputStream();
        result = toString(input);
        input.close();
        conn.disconnect();
    } else {
        throw new Exception("connect failed");
    }

    return result;
}

From source file:GCD.java

/**
* Returns the Greatest Common Divisor of two
* numbers that are passed into the method.
*
* @param a the first integer for comparison.
* @param b the second integer for comparison.
* @throws NegativeNumberException if the input given is negative.
* @returns the Greatest Common Divisor./*from  w  w  w. j av  a2s  .c om*/
*/
public static int gcd(int a, int b) throws Exception {
    // throw an exception if the input is negative
    if (a < 0 || b < 0) {
        throw new Exception("The user has entered a " + "negative number.");
    }

    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}

From source file:Main.java

/**
 * Convert string to XML {@link Document}
 *
 * @param string - string to be converted
 * @return - {@link Document}/*from   w ww .  j a v  a2 s  . com*/
 * @throws Exception - if {@link DocumentBuilder} is not initialized
 */
public static Document stringToXml(String string) throws Exception {
    if (builder == null) {
        throw new Exception("DocumentBuilder is null.");
    }
    return builder.parse(new InputSource(new ByteArrayInputStream(string.getBytes("UTF-8"))));
}

From source file:Main.java

public static boolean isImage(Blob blob) throws Exception {
    InputStream in = null;/*from www  . ja v  a 2 s  .co  m*/
    byte[] bytes = new byte[8];
    try {
        in = blob.getBinaryStream();

        in.read(bytes);
        return isImage(bytes);
    } catch (Exception ex) {
        throw new Exception(ex.toString());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                throw new Exception(ex.toString());
            }
        }
        bytes = null;
    }
}

From source file:com.att.pirates.util.DBUtility.java

public static Connection getDBConnection() throws SQLException, Exception {
    Connection conn = null;// ww w .j  a  v a 2 s.  com
    PropertyReader reader = new PropertyReader();
    String forName = reader.readPropertyByName(PiratesConstants.CLASSFORNAME);
    if (StringUtils.isBlank(forName) || StringUtils.isEmpty(forName)) {
        throw new Exception("forName is empty from property file");
    }

    String serverName = reader.readPropertyByName(PiratesConstants.SERVERNAME);
    String databaseName = reader.readPropertyByName(PiratesConstants.DATABASENAME);
    String user = reader.readPropertyByName(PiratesConstants.USER);
    String password = reader.readPropertyByName(PiratesConstants.PASSWORD);

    if (StringUtils.isBlank(serverName) || StringUtils.isEmpty(databaseName) || StringUtils.isBlank(user)
            || StringUtils.isEmpty(password)) {
        throw new Exception("connection string is empty from property file");
    }

    String url = serverName + ";" + "databaseName=" + databaseName;
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    conn = DriverManager.getConnection(url, user, password);
    return conn;
}

From source file:Main.java

/**
 * Determine whether a single child is available of a particular type.
 * @return true if the specified child element is present
 * @throws Exception if the child is present multiple times.
 *//*from  w ww. j a  va 2 s  . co  m*/
public static boolean hasChild(Element element, String child) throws Exception {
    NodeList nodes = element.getChildNodes();
    Element ret = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equals(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (ret != null) {
                throw new Exception("Child element '" + child + "' present multiple times");
            } else {
                ret = (Element) childNode;
            }
        }
    }
    return ret != null;
}

From source file:Main.java

public static boolean compareNode(Node nQ, Node nN, Boolean considerLength, Map<String, Node> qvars)
        throws Exception {
    /*System.out.println("current query tree:");
    try {//from   www  .  ja v a 2  s.c  o  m
     System.out.println(printDocument(nQ));
    }catch (Exception e) {
     e.printStackTrace();
    }
    System.out.println("current comp tree:");
    try {
     System.out.println(printDocument(nN));
    }catch (Exception e) {
     e.printStackTrace();
    }// END OF DEBUG output XML */
    if (qvars == null) {
        throw new Exception("qvars array must not be null");
    }
    if (nQ.hasChildNodes()) {
        int nQChildLength = nQ.getChildNodes().getLength();
        if (nN.hasChildNodes() && (!considerLength || nQChildLength == nN.getChildNodes().getLength())) {
            //loop through all childnodes
            for (int i = 0; i < nQChildLength; i++) {
                //System.out.println("recurse to "+ nQ.getChildNodes().item( i )+"vs"+nN.getChildNodes().item( i )); //DEBUG output XML
                if (!compareNode(nQ.getChildNodes().item(i), nN.getChildNodes().item(i), considerLength,
                        qvars)) {
                    return false;
                }
            }
        }
    }
    //check for qvar descendant, add to qvar hashmap for checking (required for checking multiple qvars)
    if (nQ.getNodeName().equals("mws:qvar")) {
        String qvarName = nQ.getAttributes().getNamedItem("name").getNodeValue();
        if (qvars.containsKey(qvarName)) {
            return compareNode(qvars.get(qvarName), nN, considerLength, qvars);
        } else {
            qvars.put(qvarName, nN);
            return true;
        }
    } else {
        //Attributes are ignored; child nodelists are not equal in length and considerlength is false OR reached lowest level: therefore check nodevalue
        if (nQ.getNodeName().equals(nN.getNodeName())) {
            try {
                return nQ.getNodeValue().trim().equals(nN.getNodeValue().trim());
            } catch (NullPointerException e) {
                //NodeValue does not exist
                return true;
            }
        } else {
            return false;
        }
    }
}

From source file:Main.java

public static File getFile(Context context, Uri uri, boolean forceCreation) {

    if (!forceCreation && "file".equalsIgnoreCase(uri.getScheme())) {
        return new File(uri.getPath());
    }/*from  ww  w  .jav  a  2  s .  c o  m*/

    File file = null;

    try {
        File root = context.getFilesDir();
        if (root == null) {
            throw new Exception("data dir not found");
        }
        file = new File(root, getFilename(context, uri));
        file.delete();
        InputStream is = context.getContentResolver().openInputStream(uri);
        OutputStream os = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int cnt = is.read(buf);
        while (cnt > 0) {
            os.write(buf, 0, cnt);
            cnt = is.read(buf);
        }
        os.close();
        is.close();
        file.deleteOnExit();
    } catch (Exception e) {
        Log.e("OpenFile", e.getMessage(), e);
    }
    return file;
}

From source file:Main.java

private static void checkResponse(int[] response, int expectedLength) throws Exception {
    checkResponse(response);/*from   w  w  w . ja v  a 2  s . com*/
    if (response.length != expectedLength)
        throw new Exception("Unexpected response length.");
}