글쓴이 :박재성 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구문의 정확한 의미를 모르겠습니다. 앙앙~~
알려주세요~~~