Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level14.lesson08.bonus01;
- import java.util.ArrayList;
- import java.util.List;
- /* Нашествие эксепшенов
- Заполни массив exceptions 10 различными эксепшенами.
- Первое исключение уже реализовано в методе initExceptions.
- */
- public class Solution
- {
- public static List<Exception> exceptions = new ArrayList<Exception>();
- public static void main(String[] args)
- {
- initExceptions();
- for (Exception exception : exceptions)
- {
- System.out.println(exception);
- }
- }
- private static void initExceptions()
- { /*1. (ArithmeticException: / by zero)*/
- try
- {
- float i = 1 / 0;
- } catch (Exception e)
- {
- exceptions.add(e);
- }
- /*2. ArrayIndexOutOfBounds Exception*/
- try{
- int a[]=new int[10];
- a[11] = 9;
- }
- catch(ArrayIndexOutOfBoundsException e){
- exceptions.add(e);
- }
- /*3. NumberFormatException*/
- try{
- int num=Integer.parseInt ("XYZ") ;
- }catch(NumberFormatException e){
- exceptions.add(e);
- }
- /*4. StringIndexOutOfBoundsException*/
- try{
- String str="easysteps2buildwebsite";
- char c = str.charAt(0);
- c = str.charAt(40);
- }catch(StringIndexOutOfBoundsException e){
- exceptions.add(e);
- }
- /*5. NullPointerException*/
- try{
- String str=null;
- System.out.println (str.length());
- }catch(NullPointerException e){
- exceptions.add(e);
- }
- /*6. NegativeArraySizeException*/
- try {
- int i = -1;
- int[] array = new int[i];
- }
- catch (NegativeArraySizeException e) {
- exceptions.add(e);
- }
- /*7. ClassCastException*/
- try {
- Object i = Integer.valueOf(42);
- String s = (String)i;
- }
- catch (ClassCastException e) {
- exceptions.add(e);
- }
- /*8. ArrayStoreException*/
- try
- {
- Object x[]=new String[3];
- x[0]=new Integer(10);
- }
- catch (ArrayStoreException e) {
- exceptions.add(e);
- }
- /*9. IllegalThreadStateException*/
- try
- {
- Thread d1 = new Thread();
- d1.start();
- d1.start();
- }
- catch (IllegalThreadStateException e) {
- exceptions.add(e);
- }
- try
- {
- throw new SecurityException ();
- }
- catch (SecurityException e) {
- exceptions.add(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement