본문 바로가기

Java

강제적으로 발생시킨 예외 처리

public class ExceptionEx {

 

           /**

            @param args

            */

           public static void main(String[] args) {

                     // TODO Auto-generated method stub

                     System.out.println("프로그램 시작");

                    

                     int a = Integer.parseInt(args[0]);

                     if (a==0){

                                System.out.println("0 입력하면 안됩니다");

                                System.exit(0);

                     }

                     int num = 3 / a;

                    

 

                     System.out.println("프로그램 종료");

           }

 

}

프로그램 시작

프로그램 종료

 

//강제적으로 발생시킨 예외 처리

public class ExceptionEx {

 

           /**

            @param args

            */

           public static void main(String[] args) {

                     // TODO Auto-generated method stub

                     System.out.println("프로그램 시작");

                    

                     // ArrayIndexOfBoundsException

                     // NumberFormatException

                     // ArithmeticException

                    

                     // 1. if

                     // 2. try ~ catch

          

                                          try {

                                                     int a = Integer.parseInt(args[0]);

                                                     int num = 3 / a;

                                          catch (NumberFormatException e) {

                                                     // TODO Auto-generated catch block

                                                     e.printStackTrace();

                                          }

                               

                    

          

//                  try{

//                             int a = Integer.parseInt(args[0]);

//                             int num = 3 / a;

//                 

//                  } catch(ArrayIndexOutOfBoundsException e){

//                             System.out.println("초기값을 입력하세야합니다");

//                  } catch(NumberFormatException e) {

//                             System.out.println("입력된 값이 숫자이어야 합니다");

//                  } catch(ArithmeticException e){

//                             System.out.println("0 입력하시면 안됩니다");

//                  } catch(Exception e) {

//                             System.out.println("잘못된 입력입니다");

//                  }

//                  System.out.println("프로그램 종료");

           }

 

}

프로그램 시작

Exception in thread "main" java.lang.ArithmeticException: / by zero

           at ExceptionEx.main(ExceptionEx.java:20)

 

'Java' 카테고리의 다른 글

이메일 마스킹  (0) 2017.01.25
Arrays 실습  (0) 2013.03.20
StringTokenizer  (0) 2013.03.20
Calendar 실습  (0) 2013.03.20
Calendar  (0) 2013.03.20