예외는 공통적으로 Exception을 상속받는다. 때문에 모든 예외를 받아 내고 싶을 경우는 Exception 하나로 처리가 가능 하다 ( 다형성의 특징)
기존 예외 이외에도 새로운 예외를 만들 수 있다.
한가지 규칙이 있다면 반드시 [Exception | RuntimeException] 클래스를 상속 받아야 한다.
// 1. 예약어 예외(실행 예외)
try {
model.reservedCheck(id);
req.setAttribute("id",id);
}catch(ReservedException e) {
//System.out.println(e.toString());// 간단한 예외 메시지
e.printStackTrace();// 개발시 사용하다가, 개발이 완료되고 나면 지워줘야 한다.
req.setAttribute("msg", id+" 는, 예약어 입니다.");
page = "index.jsp";
}
// 2 . 성인 예외 (일반예외)
try {
model.adultCheck(age);
req.setAttribute("age",age);
} catch (NumberFormatException e) {
e.printStackTrace();
page= "index.jsp";
req.setAttribute("msg","숫자 외의 문자를 넣지 마세요");
e.printStackTrace();
} catch (AdultException e) {
e.printStackTrace();
page= "index.jsp";
req.setAttribute("msg", "20세 미만은 가입 할 수 없습니다.");
}
- ArrayList : 배열과 기본적으로 같다.
- 크기가 무제한 이다.
- <> 은 제너릭이라고 하는데, 특정 데이터 타입만 받겠다고 하는 선언이다.
- 제너릭에 데이터타입은 클래스형 데이터 타입만 가능 하다(int -> integer)
private static ArrayList<String> list = new ArrayList<String>(10);
public ArrayList<String> getList(){
return list;
}
public void addList(String todo) {
// array[0] = todo;
list.add(todo); // list의 가장 뒤에 들어간다.
// list.add(0,todo) <- 특정 인덱스에 넣을수도 있지만 이렇게 사용하는 경우는 드물다.
System.out.println("0 번 인덱스의 값 : "+list.get(0));
}
public void delList(String idx) {
//list.remove(index) : 지운값 반환
//list.remove(value) : 지웠는지 여부 반환
int index = Integer.parseInt(idx);
String val = list.remove(index);
System.out.println(index+" 인덱스의 값 지움 : "+val);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
dual(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
dual(req,resp);
}
// get 으로 오던지 post로 오던지 무조건 이 메서드에서 처리(예외처리 필수)
private void dual(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
*resp.sendRedirect(ctx); //redirect 요청은 절대 경로로 시작 한다.
(context 경로를 다 지워버리기 때문에 다시 입력해준다.)
List와 같은 부모(인터페이스)를 상속받기 때문에 사용 형태가 비슷하다
구현되어야 하는 메서드가 정해져있기때문
차이점은 Vector는 사용중에 다른이의 접근을 제한한다.
//추가
public void addList(String todo) {
list.add(todo);
}
public Vector<String> getList(){
return list;
}
//삭제
public void delList(int idx) {
list.remove(idx);
}
//비우기
public void clearList() {
if(list.isEmpty()==false) {// 비워져 있지 않으면...
list.clear(); // 비운다.
}
}
//수정
public void setList(int idx, String val) {
list.set(idx, val); // idx 인덱스에 값을 val로 덮어써라
}