본문 바로가기

Web Dev/JAVA

예외처리 오늘 분 // 이해필요



import java.io.IOException;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


/**

 * @author

 */

public class ExceptionTest3 {


    private static final Logger logger = LoggerFactory.getLogger(ExceptionTest3.class);

    

    public static void main(String[] args) {

        // TODO  omosiroi _to

        int[] myarr = new int[3];

        try{

            System.out.println("入力します");

            myAssign(myarr,100,0);

            System.out.println("入力しました。");

        }catch(ArrayIndexOutOfBoundsException e){

            System.out.println("入力ができませんでした。");

            //System.out.println("例外は"+e+"です。");

            logger.error("代入できませんでした。,"+e);

            e.printStackTrace();

        }catch(IncompatibleClassChangeError e){

            

        } catch (IOException e) {

            e.printStackTrace();

        }finally{

            System.out.println("finally");

        }

        System.out.println("終了しました。");

    }

    static void myAssign(int[] arr,int index, int value)throws IOException{

        if(arr.length<=index){ //自分が例外を投げるのができる場合

            String msg ="The index was over the array lenght:"+ arr.length+":done;"+index;//後の問題に予防する

            throw new IllegalArgumentException(msg);

            //throw new IOException(msg) がエラーになる。 -->throws IOException;

            //--> try{

                //throw new IOException(msg);

            

        }//原因をつかむ

        System.out.println("myAssignにきました。");

        arr[index] = value; //runtime error 

        //method 

        System.out.println("myAssignから帰ります。");

    }

}

'Web Dev > JAVA' 카테고리의 다른 글

제네릭 , 콜렉션프레임워크 (1) -코드  (0) 2015.05.15
오늘의 java 예외처리  (0) 2015.05.12
목이 뻐근타  (0) 2015.04.30
주말엔 취미로 프로그래밍  (0) 2015.04.26
배열가지고놀기  (0) 2015.04.24