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

public static String byte2hex(byte[] bytes) throws Exception {
    try {// www .  ja va2  s  .  com
        String hs = "";
        String stmp = "";
        for (int i = 0; i < bytes.length; i++) {
            stmp = (java.lang.Integer.toHexString(bytes[i] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    } catch (Exception e) {
        throw new Exception(e);
    }
}

From source file:Main.java

/**
 * /*from   www .  j  a v  a 2 s .  c  om*/
 * @param <T>
 * @param items
 * @param offset
 *            - 0 based
 * @param length
 *            - 1 based
 * @return
 * @throws UtilityException
 */
public static <T> T[] slice(T[] items, int offset, int length) throws Exception {
    if (null == items || items.length <= 0) {
        return null;
    }
    if (items.length < length) {
        throw new Exception("Array index out of bound : " + length);
    }
    if ((offset + length) > items.length) {
        throw new Exception("Array index out of bound : " + (offset + length));
    }

    return Arrays.<T>copyOfRange(items, offset, (offset + (length - 1)));
}

From source file:Main.java

public static int getBluetoothState() throws Exception {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        throw new Exception("bluetooth device not found!");
    } else {//from  w  ww  .  ja va2 s. c  o  m
        return bluetoothAdapter.getState();
    }
}

From source file:Main.java

public static int getWifiState(Context context) throws Exception {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager != null) {
        return wifiManager.getWifiState();
    } else {/*from www  .  j  a  v  a  2 s  . c  o m*/
        throw new Exception("wifi device not found!");
    }
}

From source file:Main.java

/**
 * Carica il DOM a partire da un file contenente un documento XML
 * //  w  w  w .  j a v  a2s . c  om
 * @throws Exception
 */
public static Document caricaDOMDocument(File fileXML) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document domDocument = builder.parse(fileXML);
    if (domDocument == null) {
        throw new Exception("Errore in lettura del Document del file XML di configurazione");
    }
    return domDocument;
}

From source file:Main.java

public static <K, V> Map<K, V> collectionToMap(Collection<V> collection, String key) {
    Map<K, V> map = null;/*from  ww  w . j a  va 2  s .com*/
    Iterator<V> iterator = collection.iterator();
    try {
        if (key == null)
            throw new Exception("no key filed");
        else {
            map = new HashMap<K, V>();
            while (iterator.hasNext()) {
                V v = iterator.next();
                Class<? extends Object> c = v.getClass();
                Field field = c.getField(key);
                field.setAccessible(true);
                map.put((K) field.get(v), v);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return map;
}

From source file:Main.java

public static String GetStringValueForNode(Node item) throws Exception {
    if (item instanceof Element) {
        StringBuilder builder = new StringBuilder();
        NodeList children = item.getChildNodes();
        for (int index = 0; index < children.getLength(); index++)
            builder.append(children.item(index).getNodeValue());

        // return...
        return builder.toString();
    } else//from www.ja  va  2  s .  c  om
        throw new Exception(String.format("Cannot handle '%s'.", item.getClass()));
}

From source file:Main.java

public static String getFirstValueFromXPath(Element parent, String xpath) throws Exception {
    try {//from  w w w  .j a v  a 2  s.c  o  m
        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 long NonSearchingJavaToUnoType(Object obj, boolean errorIfMissing) throws Throwable {
    Class<?> firstCls = obj.getClass();
    String clsName = firstCls.getName();
    Long unoTypePtr = unoTypes.get(clsName);
    if (unoTypePtr == null) {
        if (errorIfMissing) {
            String msg = "NonSearchingJavaToUnoType: Could not find uno type for " + clsName;
            throw new Exception(msg);
        } else {// w  ww. ja  va2s  .  c o m
            return 0L;
        }
    }
    return (long) unoTypePtr;
}

From source file:Main.java

public static int getNumber(String s1) throws Exception {
    Pattern pattern = Pattern.compile("([\\-0-9]*)[,]*.*");
    Matcher m = pattern.matcher(s1);
    if (m.find()) {
        return Integer.parseInt(m.group(1));
    }/* w  w  w .  j  a v  a 2 s  . c  o m*/

    throw new Exception("Not valid input");
}