Advertisement
lpuarmy

Exception | OOPE

Jun 15th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. /*
  2.  * author : yufieko
  3.  */
  4.  
  5. import java.util.*;
  6. public class Excep2 {
  7.     public static void main(String[] args) {
  8.         // ArithmeticException
  9.         try{
  10.             System.out.println("ArithmeticException\n > 10/0");
  11.             System.out.println(10/0);
  12.         }catch (ArithmeticException e){
  13.             System.out.println("Error : " + e);
  14.         }
  15.         // ArrayStoreException
  16.         try{
  17.             System.out.println("\nArrayStoreException\n > Object -> String -> int (array)");
  18.             Object x[] = new String[3];
  19.             x[0] = new Integer(0);
  20.         }catch (ArrayStoreException e){
  21.             System.out.println("Error : " + e);
  22.         }
  23.         // ClassCastException
  24.         try{
  25.             System.out.println("\nClassCastException\n > Object -> Integer -> String (class casting)");
  26.             Object x = new Integer(7);
  27.             System.out.println((String)x);
  28.         }catch (ClassCastException e){
  29.             System.out.println("Error : " + e);
  30.         }
  31.         // ArrayIndexOutOfBoundsException
  32.         try{
  33.             System.out.println("\nArrayIndexOutOfBoundsException\n > new array[3] -> array[4]");
  34.             int a[] = new int[3];
  35.             a[4] = 77;
  36.         }catch (ArrayIndexOutOfBoundsException e){
  37.             System.out.println("Error : " + e);
  38.         }
  39.         // StringIndexOutOfBoundsException
  40.         try{
  41.             System.out.println("\nStringIndexOutOfBoundsException\n > String (3) -> String (4)");
  42.             String a = "aku";
  43.             char b = a.charAt(4);
  44.         }catch (StringIndexOutOfBoundsException e){
  45.             System.out.println("Error : " + e);
  46.         }
  47.         // NegativeArraySizeException
  48.         try{
  49.             System.out.println("\nNegativeArraySizeException\n > array[-2]");
  50.             int a[] = new int[-2];
  51.         }catch (NegativeArraySizeException e){
  52.             System.out.println("Error : " + e);
  53.         }
  54.         // NoSuchElementException
  55.         try{
  56.             System.out.println("\nNoSuchElementException\n > vector.firstElement");
  57.             Vector vect = new Vector();
  58.             System.out.println(vect.firstElement());
  59.         }catch (NoSuchElementException e){
  60.             System.out.println("Error : " + e);
  61.         }
  62.         // NullPointerException
  63.         try{
  64.             System.out.println("\nNullPointerException\n > parseInt(\"abc\")");
  65.             String d = null;
  66.             System.out.println(d.toString());
  67.         }catch (NullPointerException e){
  68.             System.out.println("Error : " + e);
  69.         }
  70.         // NumberFormatException
  71.         try{
  72.             System.out.println("\nNumberFormatException\n > parseInt(\"4.555\")");
  73.             String number = "4.555";
  74.             Integer.parseInt(number );
  75.         }catch (NumberFormatException e){
  76.             System.out.println("Error : " + e);
  77.         }
  78.     }
  79. }
  80.  
  81. // http://lpuarmy.blogspot.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement