728x90
반응형
package chap9;
/*
* 다중 catch 구문
* - 한개의 try블럭에 여러개의 catch블럭이 존재.
* - try블럭내에서 발생 가능한 예외가 여러 종류인 경우, 예외별로 예외처리 가능
* - 상위 예외클래스는 catch 구문의 하단에 배치해야 한다.
*/
public class ExceptionEx2 {
public static void main(String[] args) {
try {
String str = null; //객체없음.
// System.out.println(str.charAt(0)); //NullPointerException 예외발생
//int <= Integer.parseInt(숫자문자열)
// System.out.println(Integer.parseInt("abc")); //NumberFormatException 예외발생
// System.out.println(10/0); //ArithmeticException 예외발생
System.out.println(10/1);
Integer i = (Integer)((Object)"123"); //ClassCastException 예외발생
System.out.println(args[0]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("command 라인에 파라미터를 입력하세요");
} catch (NullPointerException e) {
System.out.println("전산부 전화요망. 문자열 값이 없음");
} catch (NumberFormatException e) {
System.out.println("숫자만 가능합니다");
} catch (Exception e) {
System.out.println("전산부 전화 요망:1234");
e.printStackTrace(); //기본오류메시지 출력
System.out.println(e.getMessage()); //메시지 출력
}
System.out.println("프로그램 종료");
}
}
728x90
반응형
'study > Java' 카테고리의 다른 글
[Java] 26. Exception4 (throws) (0) | 2022.03.22 |
---|---|
[Java] 26. Exception3 (finally) (0) | 2022.03.22 |
[Java] 26. Exception1 (try, catch) (0) | 2022.03.22 |
[Java] 25. enum (0) | 2022.03.22 |
[Java] 24. Test3 풀이 (구동클래스를 실행하였을때 다음의 결과가 나오도록 관련 클래스를 구현하기) (0) | 2022.03.22 |