1. Exception

 1) Riun time 때 발생 할 수 있는 사용자의 에러를 처리 해 주기위해 사용한다.

 2) exception의 구조


   - RuntimeException(Unchecked Exception)의 경우 try~catch가 없어도(사용자가 조심하면된다.) 상관없지만 일반 Exception의 경우는 반드시 try~catch이 필요(컴파일 에러)하다(Checked Exception)

 

  3) Exception의 처리 문구 확인

   - String getmessage() : 개발자가 작성한 오류문구를 출력한다. 사용자에게 보여줄 용도

   - void printStackTrace() : 에러 죵류와 에러가 발생한 곳을 표시한다. 개발자용

   - String toString () : 에러의 종류를 출력. 개발자용

 

  4) Try ~ catch

   - try에서 예외를 검사하고 발생 시 catch 구문이 실행된다. 예외가 발생하지 않으면 catch문은 처리되지 않는다.

   - 발생한 예외는 호출한 곳에서 처리가 가능하다..

   - 예외는 자신의 예외만을 catch 할 수 있다. 여러개의 오류가 예상되면 여러개의 catch를 쓰면된다.

   - 예상할수 없는 예외를 잡는 방법은 catch(Exception ex)를 사용한다. 모든 예외의 부모형으로 형변환이 가능하기 때문에 사용하는것으로 catch문 들의 가장 마지막에 사용한다.(절자적으로 감사하기때문에..

 public class ExceptionDemo2 {
 public static void main(String[] args) {
  a();
 }
 static void a(){
  b();
 }
 static void b(){
  try{
   c();
  }catch(ArrayIndexOutOfBoundsException e){ 
   System.out.println("여기서 ArrayIndexOutOfBoundsException"); 
  }catch(NullPointerException ex){
   System.out.println("여기서 NullPointerException");
  }catch(NegativeArraySizeException ex){
   System.out.println("여기서 NegativeArraySizeException");
  }catch(Exception ex){   // 가장 아래에 써야한다.
   System.out.println("내가 진짜다");
  }
 }
 static void c(){
  d();
 }
 static void d(){
  System.out.println(5 / 0);
 }
}

 

 5) RuntimeException

  -

 6) Declaring Exceptions (throws)

  - method()안에 throw된 exception이 있다는 걸 표시하는 것으로 내부에 throw된 exception이 checked 인지 uncheched인지 확인하여 try~catch를 사용해야한다.

 public class ExceptionDemo3 {
 public static void main(String[] args) {
  Student chulsu = null;
  try{
  chulsu = new Student(110);
  System.out.println(chulsu);
  }catch(RuntimeException e){
   javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
  }
 }
}
class Student{
 private int kor;
 public Student(int kor) {
  if(kor >= 0 && kor <= 100) this.kor = kor; 
  else throw new RuntimeException("점수 제대로 입력해라.");
  
 }
 @Override
 public String toString(){
  return String.format("kor = %d\n", this.kor); 
 }
}

 public class ExceptionDemo3 {
 public static void main(String[] args) {
  Student chulsu = null;
  try{
  chulsu = new Student(110);
  System.out.println(chulsu);
  }catch(Exception e){
   javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
  }
 }
}
class Student{
 private int kor;
 public Student(int kor) throws Exception{//t선언
  if(kor >= 0 && kor <= 100) this.kor = kor; 
  else throw new Exception("점수 제대로 입력해라.");
  
 }
 @Override
 public String toString(){
  return String.format("kor = %d\n", this.kor); 
 }
}
Unchecked Exception (Runtime error) Checked Exception (Compile error) 

 

 6) Finally

  - try~catch 이후에 해야할 것들을 정의 한다.

  - ex) conn.close() //디비 접속 해제

 

 7) Exception 생성

  - cheched 인가 uncheched인가

  - 생성자 Overrind을 할건지 안할지

 public class KoreanException extends RuntimeException { // unchecked exception 생성
 public KoreanException(String msg){
  super(msg);
 }
}

 

 

2. Assertions

  1) JAVA에서 지원하는 기본적인 디버깅 방법이다.

 public class AssertionDemo{  //run as 의 argument에 vm입력값에 옵션 -ea를 설정한 후 출력확인,
 public static void main(String[] args) {
  int sum = 0, i;
  for(i = 1 ; i <= 10 ; i++){
   sum += 1;
  }
  assert i == 11 :  "i = " + i;
  assert sum > 50 : "sum 값에 이상이 있습니다";
  System.out.println("sum = " + sum);
 }
}

+ Recent posts