public class Gamja {
public static void main(String[] args) {
int a = 5, b = 0;
try {
System.out.print(a / b);
}
catch (ArithmeticException e) {
System.out.print("출력1");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.print("출력2");
}
catch (NumberFormatException e) {
System.out.print("출력3");
}
catch (Exception e) {
System.out.print("출력4");
}
finally {
System.out.print("출력5");
}
}
}
catch는 한번만 실행, 그 아래 catch는 모두 건너뜀
finally는 try, catch 여부와 관계 없이, try-catch 이후 무조건 실행
catch는 instanceof를 사용한 syntactic sugar이다.
// catch가 내부적으로 하는 일 (개념적으로)
if (e instanceof ArithmeticException) {
// 첫 번째 catch 블록
} else if (e instanceof ArrayIndexOutOfBoundsException) {
// 두 번째 catch 블록
} else if (e instanceof Exception) {
// 마지막 catch 블록
}
다음 Java 코드의 출력 결과를 쓰시오.
java
시험장 코드복사
public class Gamja {
public static void main(String[]args) {
System.out.println(calc("5"));
}
static int calc(int value) {
if(value<= 1)return value;
return calc(value- 1)+ calc(value- 2);
}
static int calc(Stringstr) {
int value= Integer.valueOf(str);
if(value<= 1)return value;
return calc(value- 1)+ calc(value- 3);
}
}
다음 Java 코드의 출력 결과를 쓰시오.
java
시험장 코드복사
class Parent {
static int total= 0;
int v= 1;
public Parent() {
total+= (++v);
show();
}
public void show() {
total+= total;
}
}
class Child extends Parent {
int v= 10;
public Child() {
v+= 2;
total+= v++;
show();
}
@Override
public void show() {
total+= total* 2;
}
}
class Gamja {
public static void main(String[]args) {
new Child();
System.out.println(Parent.total);
}
}