¿¹¿Üó¸®
//ExceptionError1.java class ExceptionError1 { public static void main(String args[]) { try { System.out.println("¸Å°³º¯¼ö·Î ¹ÞÀº µÎ °³ÀÇ °ª"); int a = Integer.parseInt(args[0]); // ¹®ÀÚ¿ °ªÀ» Á¤¼ö·Î º¯È¯ int b = Integer.parseInt(args[1]); System.out.println(" a = " + a + " b = " + b ); System.out.println(" a¸¦ b·Î ³ª´« ¸ò = " + (a/b) ); System.out.println("³ª´°¼ÀÀÌ ¿øÇÒÈ÷ ¼öÇàµÇ¾ú½À´Ï´Ù."); } catch(ArithmeticException e) { System.out.println("=================================="); System.out.println("ArithmeticException ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("=================================="); System.out.println("ArrayIndexOutOfBoundsException ó¸® ·çƾ"); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } catch(NumberFormatException e) { System.out.println("=================================="); System.out.println("NumberFormatException ó¸® ·çƾ"); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } finally { System.out.println("=================================="); System.out.println("¿¹¿Ü 󸮸¦ ³¡³»°í finally ºí·°À» ¼öÇàÇÕ´Ï´Ù"); } } } //ExceptionError2.java class ExceptionError2 { public static void main(String args[]) { try { System.out.println("¸Å°³º¯¼ö·Î ¹ÞÀº µÎ °³ÀÇ °ª"); int a = Integer.parseInt(args[0]); // ¹®ÀÚ¿ °ªÀ» Á¤¼ö·Î º¯È¯ int b = Integer.parseInt(args[1]); System.out.println(" a = " + a + " b = " + b ); System.out.println(" a¸¦ b·Î ³ª´« ¸ò = " + (a/b) ); System.out.println("³ª´°¼ÀÀÌ ¿øÇÒÈ÷ ¼öÇàµÇ¾ú½À´Ï´Ù."); } catch(Exception e) { System.out.println("=================================="); System.out.println("Exception °ü·Ã ¸ðµç ¿¹¿Ü ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } finally { System.out.println("=================================="); System.out.println("¿¹¿Ü 󸮸¦ ³¡³»°í finally ºí·°À» ¼öÇàÇÕ´Ï´Ù"); } } } //ExceptionSearch.java class ExceptionSearch { static int a, b; public static void main(String args[]) { try { a = Integer.parseInt(args[0]); // ¹®ÀÚ¿ °ªÀ» Á¤¼ö·Î º¯È¯ b = Integer.parseInt(args[1]); System.out.println("¸Å°³º¯¼ö·Î ÀԷ¹ÞÀº µÎ °³ÀÇ °ª :"); System.out.println(" a = " + a + " b = " + b ); System.out.println("=============================="); System.out.println("a() ¸Þ¼Òµå È£Ãâ Àü"); a(); System.out.println("a() ¸Þ¼Òµå È£Ãâ ÈÄ"); } catch(Exception e) { System.out.println("Exception ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } finally { System.out.println("¸Þ¼Òµå main()ÀÇ finally ºí·Ï ¼öÇà"); } } public static void a() { try { System.out.println("b() ¸Þ¼Òµå È£Ãâ Àü"); b(); System.out.println("b() ¸Þ¼Òµå È£Ãâ ÈÄ"); } catch(ArithmeticException e) { System.out.println("ArithmeticException ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } finally { System.out.println("¸Þ¼Òµå a()ÀÇ finally ºí·Ï ¼öÇà"); } } public static void b() { try { System.out.println("c() ¸Þ¼Òµå È£Ãâ Àü"); c(); System.out.println("c() ¸Þ¼Òµå È£Ãâ ÈÄ"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } finally { System.out.println("¸Þ¼Òµå b()ÀÇ finally ºí·Ï ¼öÇà"); } } public static void c() { try { System.out.println(" a¸¦ b·Î ³ª´« ¸ò = " + (a/b) ); } catch(ClassCastException e) { System.out.println("ClassCastException ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } finally { System.out.println("=============================="); System.out.println("¸Þ¼Òµå c()ÀÇ finally ºí·Ï ¼öÇà"); } } } //ExceptionGen.java class ExceptionGen { public static void main(String args[]) { try { a(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("=================================="); System.out.println("ArrayIndexOutOfBoundsException ó¸® ·çƾ"); System.out.println(e + " ¹ß»ý"); } } static void a() { try { throw new NullPointerException("³Î Æ÷ÀÎÅÍ ¿¹¿Ü"); // ¿¹¿ÜÀÇ ÀÎÀ§Àû ¹ß»ý } catch(NullPointerException e) { System.out.println("=================================="); System.out.println("NullPointerException ó¸® ·çƾ"); System.out.println(e + " ¹ß»ý"); throw new ArrayIndexOutOfBoundsException("¹è¿ ÷ÀÚ ¿¹¿Ü"); // ¿¹¿Ü ó¸® ·çƾ¿¡¼ ÀÎÀ§ÀûÀ¸·Î ¿¹¿Ü¸¦ ¹ß»ý ½ÃÄÑ È£ÃâÇÑ ¸Þ¼Òµå·Î ¹Ýȯ } } } //ExceptionTrans.java class ExceptionTrans { static int a, b; public static void main(String args[]) { try { a = Integer.parseInt(args[0]); // ¹®ÀÚ¿ °ªÀ» Á¤¼ö·Î º¯È¯ b = Integer.parseInt(args[1]); System.out.println("¸Å°³º¯¼ö·Î ÀԷ¹ÞÀº µÎ °³ÀÇ °ª :"); System.out.println(" a = " + a + " b = " + b ); System.out.println("=============================="); System.out.println("a() ¸Þ¼Òµå È£Ãâ Àü"); a(); System.out.println("a() ¸Þ¼Òµå È£Ãâ ÈÄ"); } catch(ArithmeticException e) { System.out.println("ArithmeticException ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } catch(ClassCastException e) { System.out.println("ClassCastException ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } catch(Exception e) { System.out.println("³ª¸ÓÁö ¸ðµç ¿¹¿Ü ó¸® ·çƾ : "); System.out.println(e + " ¿¹¿Ü ¹ß»ý"); } finally { System.out.println("¸Þ¼Òµå main()ÀÇ finally ºí·Ï ¼öÇà"); } } public static void a() throws ClassCastException, ArrayIndexOutOfBoundsException, ArithmeticException { System.out.println("b() ¸Þ¼Òµå È£Ãâ Àü"); b(); System.out.println("b() ¸Þ¼Òµå È£Ãâ ÈÄ"); } public static void b() throws ClassCastException, ArrayIndexOutOfBoundsException { System.out.println("c() ¸Þ¼Òµå È£Ãâ Àü"); c(); System.out.println("c() ¸Þ¼Òµå È£Ãâ ÈÄ"); } public static void c() throws ClassCastException { System.out.println("ÃÖÁ¾ ¸Þ¼Òµå ¿¹¿Ü ¹ß»ý ¹®Àå ¼öÇà Àü"); System.out.println("a¸¦ b·Î ³ª´« ¸ò = " + (a/b) ); System.out.println("ÃÖÁ¾ ¸Þ¼Òµå ¿¹¿Ü ¹ß»ý ¹®Àå ¼öÇà ÈÄ"); } } //ExceptionByUser.java import java.util.*; class ExceptionByUser { public static void main(String args[]) { try { int a = Integer.parseInt(args[0]); if(a % 2 == 0) { throw new UserException1("»ç¿ëÀÚ Á¤ÀÇ ¿¹¿Ü 1"); } else { throw new UserException2("»ç¿ëÀÚ Á¤ÀÇ ¿¹¿Ü 2"); } } catch(UserException1 e) { System.out.println("UserException1 ó¸® ·çƾ : "); System.out.println(e + " ¹ß»ý"); } catch(UserException2 e) { System.out.println("UserException2 ó¸® ·çƾ : "); System.out.println(e + " ¹ß»ý"); } catch(Exception e) { System.out.println("¸ðµç ¿¹¿Ü ó¸® ·çƾ : "); System.out.println(e + " ¹ß»ý"); } } } class UserException1 extends Exception { // »ç¿ëÀÚ Á¤ÀÇ ¿¹¿Ü´Â Exception Ŭ·¡½º·ÎºÎÅÍ »ó¼Ó public UserException1(String message) { super(message); } } class UserException2 extends Exception { public UserException2(String message) { super(message); } }