¼Ò½ºÄÚµå¶óÀ̺귯¸®  ¿¹¿Ü󸮠 throw ±¸¹®  

throw ±¸¹®

1. seThrowException.java

public class UseThrowException {
   
public static void 
main(String args[]) {
      
try 

         
throw new Exception("À̰ÍÀÌ ¿¡·¯ ¸Þ½ÃÁö"
);
      } 
catch
(Exception e) {
         System.out.println(
"--Exception ¹ß»ý±¸¹®--"
);
         System.out.println(
"Á¤º¸:e.getMessage(): " 
+ e.getMessage());
         System.out.println(
"Á¤º¸:e.toString(): " 
+ e.toString());
         System.out.print(
"Á¤º¸:e.printStackTrace():"
);e.printStackTrace();
         
return
;
      } 
finally
{
         System.out.println(
"finally: °á±¹À̸®·Î ¿À´Â±º¿ä"
);
      }
   }
}

C:\examples\7. Exception>javac  UseThrowException.java
C:\examples\7. Exception>java  UseThrowException
--ArrayIndexOutOfBoundsException ¹ß»ý±¸¹®--
Á¤º¸:e.getMessage(): ¿¡·¯ ¸Þ½ÃÁö
Á¤º¸:e.toString(): java.lang.Exception: ¿¡·¯ ¸Þ½ÃÁö
Á¤º¸:e.printStackTrace():java.lang.Exception: ¿¡·¯ ¸Þ½ÃÁö
        at UseThrowException.main(UseThrowException.java:4)
finally: °á±¹À̸®·Î ¿À´Â±º¿ä

2. NeedThrowException.java

import java.net.*;
public class 
NeedThrowException {
    
public static void 
main(String args[]) {
        
try
{
            URL url = 
new URL("http://www.yahoo.co.kr"
);
        } 
catch
(Exception e) {
            System.out.println(
"--Exception "¹ß»ý±¸¹®
");
            System.out.println("
Á¤º¸:e.getMessage(): 
" + e.getMessage());
            System.out.println("
Á¤º¸:e.toString(): 
" + e.toString());
            System.out.print("
Á¤º¸:e.printStackTrace():
");e.printStackTrace();
            return;
        } finally{
            System.out.println("
finally: °á±¹ À̸®·Î ¿À´Â ±º¿ä
");
        }
    }
}

C:\examples\7. Exception>javac NeedThrowException.java
C:\examples\7. Exception>java NeedThrowException
finally: °á±¹À̸®·Î ¿À´Â±º¿ä

throw±¸¹®Àº »ç¿ëÀÚ°¡ Á÷Á¢ ¿¡·¯ À̺¥Æ®¸¦ ¹ß»ý½ÃÄѼ­ ¿¡·¯Ã³¸®¸¦ ÇÒ¶§ »ç¿ëÇÕ´Ï´Ù. ±×¸®°í ÀÌ ±¸¹®¿¡¼­ ¹ß»ýÇÑ ¿¡·¯ À̺¥Æ®´Â catch±¸¹®¿¡¼­ 󸮸¦ ÇÕ´Ï´Ù.

 



jabookÀúÀÚ¸íÇÔ
Á¦¸ñ:°´Ã¼°³³ä¼Ò½ºÄÚµå
ÀúÀÚ:ÃÖ¿µ°ü