메소드를 사용하면 두 날짜 사이의 기간(Period)을 구할 수 있다.
전체 일 수 차이를 구하려면 ChronoUnit 클래스의 Between() 메소드를 사용
LocalDate today = LocalDate.now(); // 현재 날짜
LocalDate oldDate = LocalDate.of(2021,7,1); //
Period period = oldDate.until(today); // 비교일로부터 오늘까지의 날짜 차이 계산
System.out.println(today);
System.out.println(oldDate);
System.out.println(period.getYears()); //년
System.out.println(period.getMonths()); //월
System.out.println(period.getDays()); //일
Error → 치명적인 오류
exception(예외) → 무시할 수 있는 정도의 오류
실행 중 예외가 발생하면
예외 발생 경우(예시)
예외처리 방법 1 → 정상 실행, 종료
발생한 예외에 대해 개발자가 작성한 프로그램 코드에서 대응하는 것
try-catch-finally문 사용
- 예외가 발생 안했을 때 : ① → ③ → ④
- 예외가 발생하면 : ① → ② → ③ → ④
//finally 블록은 생략 가능
try{
① //(예외가 발생할 가능성이 있는) 실행문(명령문) -> 예외 1
}catch(처리할 예외 타입 선언) {
② //(예외가 발생할 때만 실행되는) 예외처리문 -> 예외 1
}finally{
③ // 예외 발생 여부와 상관없이 무조건 실행되는 문장.
}
④ //정상실행
catch { }
예외처리 방법 2
예외를 발생 → 예외를 던졌다, throw, throws
throw new Exception();
try{
//고의 예외발생 -> DB 관련 처리시 특별한 경우
throw new Exception();
}catch(Exception e) {
}
메서드에 throws
- 메서드 선언부에서 예외를 던지면 구현부에서 그 예외가 발생이 되면 try~catch문을 사용하지 않아도 된다.
public void excuteQueryService() throws ClassNotFoundException, SQLException{
//try~catch문 생략가능
}
///exception -> 모든 예외를 처리
try{
① //(예외가 발생할 가능성이 있는) 실행문(명령문) -> 예외 1
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("기본 실행");
}
ClassNotFoundException → class 찾기
SQLException → SQL관련, Connection
try~catch문 안에 try~catch문을 쓸 수 있다.
throw IOException, SQLexception etc
//데이이스 연결(Connetion 연동), SQL처리, File입출력에 사용된다
//Servelet→Web→service
***예외(Exception)도 오버라이딩 된다.
정말 좋은 글 감사합니다!