Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * author : yufieko
- */
- import java.util.*;
- public class Excep2 {
- public static void main(String[] args) {
- // ArithmeticException
- try{
- System.out.println("ArithmeticException\n > 10/0");
- System.out.println(10/0);
- }catch (ArithmeticException e){
- System.out.println("Error : " + e);
- }
- // ArrayStoreException
- try{
- System.out.println("\nArrayStoreException\n > Object -> String -> int (array)");
- Object x[] = new String[3];
- x[0] = new Integer(0);
- }catch (ArrayStoreException e){
- System.out.println("Error : " + e);
- }
- // ClassCastException
- try{
- System.out.println("\nClassCastException\n > Object -> Integer -> String (class casting)");
- Object x = new Integer(7);
- System.out.println((String)x);
- }catch (ClassCastException e){
- System.out.println("Error : " + e);
- }
- // ArrayIndexOutOfBoundsException
- try{
- System.out.println("\nArrayIndexOutOfBoundsException\n > new array[3] -> array[4]");
- int a[] = new int[3];
- a[4] = 77;
- }catch (ArrayIndexOutOfBoundsException e){
- System.out.println("Error : " + e);
- }
- // StringIndexOutOfBoundsException
- try{
- System.out.println("\nStringIndexOutOfBoundsException\n > String (3) -> String (4)");
- String a = "aku";
- char b = a.charAt(4);
- }catch (StringIndexOutOfBoundsException e){
- System.out.println("Error : " + e);
- }
- // NegativeArraySizeException
- try{
- System.out.println("\nNegativeArraySizeException\n > array[-2]");
- int a[] = new int[-2];
- }catch (NegativeArraySizeException e){
- System.out.println("Error : " + e);
- }
- // NoSuchElementException
- try{
- System.out.println("\nNoSuchElementException\n > vector.firstElement");
- Vector vect = new Vector();
- System.out.println(vect.firstElement());
- }catch (NoSuchElementException e){
- System.out.println("Error : " + e);
- }
- // NullPointerException
- try{
- System.out.println("\nNullPointerException\n > parseInt(\"abc\")");
- String d = null;
- System.out.println(d.toString());
- }catch (NullPointerException e){
- System.out.println("Error : " + e);
- }
- // NumberFormatException
- try{
- System.out.println("\nNumberFormatException\n > parseInt(\"4.555\")");
- String number = "4.555";
- Integer.parseInt(number );
- }catch (NumberFormatException e){
- System.out.println("Error : " + e);
- }
- }
- }
- // http://lpuarmy.blogspot.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement