AkhmadGhafirin
5/16/2017 - 2:47 AM

Exception

package exception;

import java.math.BigDecimal;

/**
 * Created by ahmad on 30/04/17.
 */
public class Arithmetic extends Throwable {
    public static void main(String[] args) {
        int a = 50;
        try {
            BigDecimal x = new BigDecimal(1);
            BigDecimal y = new BigDecimal(3);
            x = x.divide(y);
            System.out.println(x.toString());
            System.out.println(a / 0);
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception: You can't divide an integer by 0!");
        }
    }
}

===========================================================================================================

package exception;

/**
 * Created by ahmad on 30/04/17.
 */
public class ArrayIndexOutOfBoundsExeption {
    public static void main(String[] args) {
        String a[] = new String[5];
        try {
            a[5] = "String";
        } catch (ArrayIndexOutOfBoundsException e){
            System.out.println("Index Array melebihi batas!");
        }
    }
}


========================================================================================================================

package exception;

/**
 * Created by ahmad on 30/04/17.
 */
public class ArrayStore {
    public static void main(String[] args) {
        try {
            Object x[] = new String[5];
            x[0] = new Integer(0);
        }catch (ArrayStoreException e){
            System.out.println("ArrayStoreException!");
        }
    }
}

===========================================================================================================

package exception;

/**
 * Created by ahmad on 29/04/17.
 */
public class Casting {
    public static void main(String[] args) {
        int valueInt = 15;
        double valueDouble = (double) valueInt;
        System.out.println("Value Double : " + valueDouble);
    }
}


===================================================================================================

package exception;

/**
 * Created by ahmad on 30/04/17.
 */
public class ClassCast {
    public static void main(String[] args) {
        try {
            Object x = new Integer(0);
            System.out.println((String)x);
        }catch (ClassCastException e){
            System.out.println("Class Cast Exception!");
        }
    }
}


==========================================================================================================

  package exception;

/**
 * Created by ahmad on 30/04/17.
 */
public class NegativeArraySize {
    public static void main(String[] args) {
        int i = -10;
        try {
            int[] x = new int[i];
        }catch (NegativeArraySizeException e){
            System.out.println("Negative Array Size Exception!");
        }
    }
}

============================================================================================================


package exception;

import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 * Created by ahmad on 30/04/17.
 */
public class NoSuchElement {
    public NoSuchElement() {
        Scanner input = new Scanner(System.in);
        System.out.print("Masukkan Bilangang : ");
        int x = input.nextInt();
        input.close();
    }

    public static void main(String[] args) {
        NoSuchElement x = new NoSuchElement();
        try {
            NoSuchElement y = new NoSuchElement();
        }catch (NoSuchElementException e){
            System.out.println("NoSuchElementException!");
        }
    }
}


========================================================================================================

package exception;

/**
 * Created by ahmad on 30/04/17.
 */
public class NullPointer {
    public static void main(String[] args) {
        try {
            String str = null;
            System.out.println(str.length());
        }catch (NullPointerException e){
            System.out.println("Null Pointer Exception!");
        }
    }
}


========================================================================================================

package exception;

/**
 * Created by ahmad on 30/04/17.
 */
public class NumberFormatExeption {
    public static void main(String[] args) {
        try {
            int number = Integer.parseInt("ABCDE");
            System.out.println(number);
        }catch (NumberFormatException e){
            System.out.println("Number format exception occurred!");
        }
    }
}


============================================================================================================


package exception;

/**
 * Created by ahmad on 30/04/17.
 */
public class StringIndexOutOfBounds {
    public static void main(String[] args) {
        try {
            String str = "abcdefghijklmnopqrstuvwxyz";
            System.out.println(str.length());
            char c = str.charAt(100);
            System.out.println(c);
        }catch (StringIndexOutOfBoundsException e){
            System.out.println("String Index Out Of Bounds Exception!");
        }
    }
}