본문 바로가기
개발/java

java Exception(PrintStream, ArrayIndexOutOfBoundsException )

by 카앙구운 2015. 8. 4.
728x90
반응형

PrintStream

해당경로에 저장할 수 있도록 한다.

--------------------------------------------------------start

package study8;

import java.io.FileNotFoundException;
import java.io.PrintStream;

public class ErrorMessagePrintEx {
 public static void main(String[] args) {
  String[] test={"JAVA","PHP","JSP"};
  String pnt="";
  PrintStream ps = null;
  for(int i=0;i<test.length;i++){
   pnt+=test[i]+"\n";
  }
  try {
   ps = new PrintStream("c:\\test.txt");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
 
  ps.println(pnt);
 }
}

-결과-

 

 


--------------------------------------------------------end

ArrayIndexOutOfBoundsException

배열관련 에러발생시 사용되는 Exception

finally는 반드시 실행된다.

--------------------------------------------------------start

package study8;

public class ArrayIndexOutOfExcetionEx1 {
 public static void main(String[] args) {
  String[] pgName = { "JAVA", "JSP", "PHP" };
  int i = 0;
  while (i < 5) {
   try {
    if(i==4)System.out.println(i/0);
    else System.out.println(pgName[i]);
   } catch (ArrayIndexOutOfBoundsException a) {
    System.out.println("array Exception");
   } catch (Exception e) {
    System.out.println("zero error");
   } finally {
    System.out.println("finally");
   }
   i++;
  }
 }
}

-결과-

 


--------------------------------------------------------end

728x90
반응형

댓글