Advertisement
jaVer404

level14.lesson08.bonus01

Jun 24th, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package com.javarush.test.level14.lesson08.bonus01;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /* Нашествие эксепшенов
  7. Заполни массив exceptions 10 различными эксепшенами.
  8. Первое исключение уже реализовано в методе initExceptions.
  9. */
  10.  
  11. public class Solution
  12. {
  13.     public static List<Exception> exceptions = new ArrayList<Exception>();
  14.  
  15.     public static void main(String[] args)
  16.     {
  17.         initExceptions();
  18.  
  19.         for (Exception exception : exceptions)
  20.         {
  21.             System.out.println(exception);
  22.         }
  23.     }
  24.  
  25.     private static void initExceptions()
  26.     {   /*1. (ArithmeticException: / by zero)*/
  27.         try
  28.         {
  29.             float i = 1 / 0;
  30.  
  31.         } catch (Exception e)
  32.         {
  33.             exceptions.add(e);
  34.         }
  35.  
  36.         /*2. ArrayIndexOutOfBounds Exception*/
  37.         try{
  38.             int a[]=new int[10];
  39.             a[11] = 9;
  40.         }
  41.         catch(ArrayIndexOutOfBoundsException e){
  42.             exceptions.add(e);
  43.         }
  44.  
  45.         /*3. NumberFormatException*/
  46.         try{
  47.             int num=Integer.parseInt ("XYZ") ;
  48.         }catch(NumberFormatException e){
  49.             exceptions.add(e);
  50.         }
  51.  
  52.  
  53.         /*4. StringIndexOutOfBoundsException*/
  54.         try{
  55.             String str="easysteps2buildwebsite";
  56.             char c = str.charAt(0);
  57.             c = str.charAt(40);
  58.         }catch(StringIndexOutOfBoundsException e){
  59.             exceptions.add(e);
  60.         }
  61.  
  62.         /*5. NullPointerException*/
  63.         try{
  64.             String str=null;
  65.             System.out.println (str.length());
  66.         }catch(NullPointerException e){
  67.             exceptions.add(e);
  68.         }
  69.         /*6. NegativeArraySizeException*/
  70.         try {
  71.             int i = -1;
  72.             int[] array = new int[i];
  73.         }
  74.         catch (NegativeArraySizeException e) {
  75.             exceptions.add(e);
  76.         }
  77.  
  78.         /*7. ClassCastException*/
  79.         try {
  80.             Object i = Integer.valueOf(42);
  81.             String s = (String)i;
  82.         }
  83.         catch (ClassCastException e) {
  84.             exceptions.add(e);
  85.         }
  86.  
  87.         /*8. ArrayStoreException*/
  88.  
  89.         try
  90.         {
  91.             Object x[]=new String[3];
  92.             x[0]=new Integer(10);
  93.         }
  94.         catch (ArrayStoreException e) {
  95.             exceptions.add(e);
  96.         }
  97.  
  98.         /*9. IllegalThreadStateException*/
  99.         try
  100.         {
  101.             Thread d1 = new Thread();
  102.             d1.start();
  103.             d1.start();
  104.         }
  105.         catch (IllegalThreadStateException e) {
  106.             exceptions.add(e);
  107.         }
  108.  
  109.         try
  110.         {
  111.             throw new SecurityException ();
  112.         }
  113.         catch (SecurityException e) {
  114.             exceptions.add(e);
  115.         }
  116.  
  117.  
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement