±Û¾´ÀÌ :¹ÚÀ缺 1999³â 7¿ù 06ÀÏ 08:11:30
¹Ø¿¡ ÀÖ´Â ¼Ò½º ÄÚµåÁß¿¡¼ catchºí·° ¾È¿¡ ÀÖ´Â returnÀÌ ¾î¶²Àǹ̰¡ ÀÖ´ÂÁö ¸ð¸£°Ú½À´Ï´Ù.
class MyException extends Exception {
public MyException() { super(); }
public MyException(String s) { super(s); }
}
class MyOtherException extends Exception {
public MyOtherException() { super(); }
public MyOtherException(String s) { super(s); }
}
class MySubException extends MyException {
public MySubException() { super(); }
public MySubException(String s) { super(s); }
}
public class Throwtest {
public static void main(String[] args) {
int i;
try {
i = Integer.parseInt(args[0]);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Must specify an argument");
return; // ¿äºÎºÐ
}
catch (NumberFormatException e) {
System.out.println("Must specify an integer argument.");
return; // ¿äºÎºÐ
}
a(i); // À§¿¡ returnÀ» ÄÚ¸àÆ® ó¸®ÇÏ¸é ¿ä±â¼ error
}
public static void a(int i) {
try {
b(i);
}
catch (MyException e) {
if (e instanceof MySubException)
System.out.print("MySubException: ");
else
System.out.print("MyException: ");
System.out.println(e.getMessage());
System.out.println("Handled at point 1");
}
}
public static void b(int i) throws MyException {
int result;
try {
System.out.print("i = " + i);
result = c(i);
System.out.print(" c(i) = " + result);
}
catch (MyOtherException e) {
System.out.println("MyOtherException: " + e.getMessage());
System.out.println("Handled at point 2");
}
finally {
System.out.println("\n");
}
}
public static int c(int i) throws MyException, MyOtherException {
switch (i) {
case 0:
throw new MyException("input too low");
case 1:
throw new MySubException("input still too low");
case 99:
throw new MyOtherException("input too high");
default:
return i*i;
}
}
}
Throwtest class ÀÇ mainÇÔ¼ö¿¡¼ µÎ°³ÀÇ catchºí·°¿¡ ¾²ÀÎ return ²ÀÀÖ¾î¾ß µÇ´Â ÀÌÀ¯¸¦
¸ð¸£°Ú½À´Ï´Ù. return¹®À» ÄÚ¸àÆ® ó¸®ÇÏ°í ÄÄÆÄÀÏÇÏ¸é ±× catch block ¹Ø¿¡ ÀÖ´Â a(i); ±¸¹®¿¡¼
i°¡ initialize ¾ÈµÆ´Ù´Â error¸¦ ³À´Ï´Ù. ¶Ç ù¹ø° return¹®¸¸ ÄÚ¸àÆ® ó¸®ÇÏ°í µÎ¹ø° return¹®Àº
³öµÎ¸é error¾øÀÌ compileÀº µÇÁö¸¸ ½ÇÇà½Ã runtime exceptionÀÌ ¹ß»ýÇÕ´Ï´Ù.
>java Throwtest 0
Exception in thread "main" java.lang.VerifyError: (class: Throwtest, method: main
signature: ([Ljava/lang/String;)V) Accessing value from uninitialized register 1
return±¸¹®ÀÇ Á¤È®ÇÑ Àǹ̸¦ ¸ð¸£°Ú½À´Ï´Ù. ¾Ó¾Ó~~
¾Ë·ÁÁÖ¼¼¿ä~~~