Example usage for java.lang ArithmeticException ArithmeticException

List of usage examples for java.lang ArithmeticException ArithmeticException

Introduction

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

Prototype

public ArithmeticException(String s) 

Source Link

Document

Constructs an ArithmeticException with the specified detail message.

Usage

From source file:Main.java

static void demoproc() {
    NullPointerException e = new NullPointerException("top layer");
    e.initCause(new ArithmeticException("cause"));
    throw e;/*from ww  w.  j a  va  2s.com*/
}

From source file:ChainExcDemo.java

static void demoproc() {
    NullPointerException e = new NullPointerException("top layer");

    e.initCause(new ArithmeticException("cause"));

    throw e;/*from w w  w  .j a  va  2  s.c  om*/
}

From source file:Main.java

public static int addAndCheck(int x, int y) {
    long s = (long) x + (long) y;
    if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
        throw new ArithmeticException("overflow: add");
    }/*from   w  ww.  j av  a  2  s.  c o m*/
    return (int) s;
}

From source file:Main.java

public static boolean test(int i) {
    if (i < 0) {
        throw new ArithmeticException("Cannot determine if a negative value is prime");
    } else if (i == 0) {
        throw new ArithmeticException("Cannot determine if zero is prime");
    } else if (i == 1) {
        throw new ArithmeticException("Cannot determine if one is prime");
    } else if (i == 2) {
        return true;
    } else {//from   w  w  w .  j  a va 2s .c om
        return unprotectedTest(i);
    }
}

From source file:Main.java

public static int[] primeFactorization(int i) {
    if (i < 0) {
        throw new ArithmeticException("Cannot determine if a negative value is prime");
    } else if (i == 0) {
        throw new ArithmeticException("Cannot determine if zero is prime");
    } else if (i == 1) {
        throw new ArithmeticException("Cannot determine if one is prime");
    } else {//from   ww w . j  a  v  a 2s. co m
        int[] returnValue = new int[i];
        int j = i;
        int x = 0;
        int n = 2;
        while (n <= j) {
            if (test(n)) {
                if (j % n == 0) {
                    returnValue[x] = n;
                    x++;
                    j = j / n;
                    n = 1;
                }
            }
            n++;
        }
        return arrayCopy(returnValue);
    }
}

From source file:Main.java

public static int mulAndCheck(int x, int y) {
    long m = ((long) x) * ((long) y);
    if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) {
        throw new ArithmeticException("overflow: mul");
    }//from  w  w  w.  ja v  a2s .c  o  m
    return (int) m;
}

From source file:Main.java

public static int subAndCheck(int x, int y) {
    long s = (long) x - (long) y;
    if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
        throw new ArithmeticException("overflow: subtract");
    }/*from w  w  w  . j av  a 2  s.co  m*/
    return (int) s;
}

From source file:Main.java

public static boolean test(long l) {
    if (l < 0) {
        throw new ArithmeticException("Cannot determine if a negative value is prime");
    } else if (l == 0) {
        throw new ArithmeticException("Cannot determine if zero is prime");
    } else if (l == 1) {
        throw new ArithmeticException("Cannot determine if one is prime");
    } else if (l == 2) {
        return true;
    } else {/*from w  ww  .  j av  a  2  s  . co  m*/
        return unprotectedTest(l);
    }
}

From source file:Main.java

public static byte toByte(long l) {
    if (l < Byte.MIN_VALUE || l > Byte.MAX_VALUE)
        throw new ArithmeticException("Value (" + l + ") cannot fit into byte");
    return (byte) l;
}

From source file:Main.java

public static short toShort(long l) {
    if (l < Short.MIN_VALUE || l > Short.MAX_VALUE)
        throw new ArithmeticException("Value (" + l + ") cannot fit into short");
    return (short) l;
}