try { 실행 코드 } catch (예외 타입 선언) { 예외 처리 코드 }
try{ ① 정상 실행 코드 ② 정상 실행 코드 ③ 정상 실행 코드 } catch(예외 타입 선언) { ~~④예외 처리 코드~~ } ⑤ try - catch 이후 코드
try { ① 정상 실행 코드 **② 예외 발생 코드** ~~③ 정상 실행 코드~~ } catch (예외 타입 선언) { ④예외 처리 코드 } ⑤ try - catch 이후 코드
try { 실행 코드 } catch (예외 타입 선언1) { // 예외 발생시 1차 방문 예외 처리 코드 1 ↓예외 타입이 안 맞으면 이동 } catch (예외 타입 선언2) { // 2차 방문 예외 처리 코드 2 }
try { // } catch (Exception e) { // 모든 예외를 처리 했기 때문에 } catch (IOException e) { // 실행 불가 }
try { // } catch (IOException e) { // IOException만 처리하고 다른 예외는 두 번째 catch 블록이 처리함 } catch (Exception e) { // IOException 이외의 예외 처리 담당 }
try { 실행 코드 } catch (예외 타입 선언) { 예외 처리 코드 } finally { 무조건 실행되는 코드 }
try{ if(예외 상황이면) { throw new RuntimeException("예외 메시지"); } // 예외를 발생시켜서 덤짐 (throw) } catch(RuntimeException e { System.out.println(e.getMesssage()); // 예외메세지 }
-throw로 만든 예외 메세지는 getMessage로 확인 가능
class Exception{ private String message; pulic Exception(String message){ this.message = message; } public String getMessage() { return message; } } class RuntimeException extends Exception{ public RuntimeException (String message) { super(message); } } Exception e = new RuntimeException("나이는 0이상 100이하만 가능합니다."); System.out.ptintln(e.getMessage());
public class ThrowsEx{ public static void method() { try{ 메소드 내부 코드 } catch(Exception e) { 예외 처리 코드 } } public static void main(String[] args) { method(); } }
public class ThrowsEx{ public static void method() throws Exception { 메소드 내부 코드 } public static void main(String[] args) { try { method(); } catch(Exception e){ 예외 처리 코드 } } }
import java.util.InputMismatchException; import java.util.Scanner; public class ParkingLot { private Car[] cars; private int idx; private Scanner sc; public ParkingLot() { cars = new Car[10]; sc = new Scanner(System.in); } public void addCar() throws RuntimeException{ if(idx == cars.length) { throw new RuntimeException("FULL"); } } public void deleteCar() throws RuntimeException { if(idx == 0) { throw new RuntimeException("EMPTY"); } } public void findCar() throws RuntimeException{ if(idx == 0) { throw new RuntimeException("EMPTY"); } } public void printAllCars() throws RuntimeException { if(idx == 0) { throw new RuntimeException("EMPTY"); } } public void manage() { while(true) { try { System.out.print("1.추가 2.제거 3.조회 4.전체목록 0.종료 >>> "); int choice = sc.nextInt(); switch(choice) { case 1: addCar(); break; case 2: deleteCar();break; case 3: findCar();break; case 4: printAllCars();break; case 0: return; default : throw new RuntimeException("Bad Request"); } } catch (InputMismatchException e){ sc.next(); System.out.println("처리 명령은 정수(1~4,0) 입니다."); } catch(RuntimeException e) { System.out.println(e.getMessage()); } } } public static void main(String[] args) { new ParkingLot().manage(); } }
Serializable 인터페이스 : 이 인터페이스를 구현하면 직렬화가 가능. serialVersionUID값을 가져야 함 (추천)
↑
Throwable 클래스 : serialVersionUID 값이 필요함
↑
Exception 클래스 : serialVersionUID 값이 필요함
↑
MyException 클래스 : serialVersionUID 값이 필요함
public class MyException extends Exception{ private static final long serialVersionUID = -7774118171104436322L; private int errorCode; public MyException(String message, int errorCode) { super(message); this.errorCode = errorCode; } public int getErrorCode() { return errorCode; } public void setErrorCode(int errorCode) { this.errorCode = errorCode; } }
import java.util.InputMismatchException; import java.util.Scanner; public class ParkingLot { private Car[] cars; private int idx; private Scanner sc; public ParkingLot() { cars = new Car[10]; sc = new Scanner(System.in); } public void addCar() throws MyException { if(idx == cars.length) { throw new MyException("FULL", 1000); // 에러코드 1000 RuntimeException의 자식이 아니기 때문에 addCar 옆에 throws MyException 추가 필요 } } public void deleteCar() throws MyException{ if ( idx == 0) { throw new MyException("EMPTY", 2000); // 에러코드 2000 } } public void manage() { while (true) { try { System.out.println("1.추가 2.삭제 0.종료 >>> "); int choice = sc.nextInt(); switch (choice) { case 1 : addCar(); break; case 2 : deleteCar(); break; case 0 : return; default : throw new RuntimeException("Bad Request"); } } catch ( MyException e) { System.out.println(e.getMessage() + "[" + e.getErrorCode() + "]"); } catch ( InputMismatchException e ) { sc.next(); System.out.println("처리 명령은 정수만 가능"); } catch ( RuntimeException e ) { System.out.println(e.getMessage()); } } } public static void main(String[] args) { new ParkingLot().manage(); } }