[ 예외처리 연습문제 ]
1. 다음 소스 코드는 컴파일시 에러가 난다. 이유는?
class ExceptionEx1 {
public static void main(String[] args)
{
try {
try { } catch (Exception e) { }
} catch (Exception e) {
try { } catch (Exception e) { }
} // try-catch의 끝
try {
} catch (Exception e) {
} // try-catch의 끝
} // main메서드의 끝
}
☞ try~catch 블럭 안의 try~catch 블럭에서 Exception e 변수명 e가 중복으로 사용되었다.
2. 다음 소스코드는 예외가 발생하는 소스코드이다. 어디서 예외가 발생하나? 그리고 그 이유는?
class ExceptionEx2 {
public static void main(String args[]) {
int number = 100;
int result = 0;
for(int i=0; i < 10; i++) {
result = number / (int)(Math.random() * 10);
System.out.println(result);
}
} // main의 끝
}
☞ result = number/(int)(Math.random()*10);
i의 값이 0부터 시작하기 때문에 난수를 발생시키더라도 0이 나오게 되어 0*10이 될 경우 0이 되므로 result에 대한 값을 number/0, 0으로 나눗셈을 하게 된다.
3. 2번 소스 코드를 예외가 발생하지 않도록 수정해 보자.
☞
class ExceptionEx2
{
public static void main(String[] args)
{
int number=100;
int result=0;
for(int i=0; i<10; i++)
{
try
{
result = number / (int)(Math.random() * 10);
System.out.println(result);
}
catch(ArithmeticException e)
{
System.out.println("0");
}
}
}
}
☞ try~catch 블럭을 사용하여 try문 안에서 실행이 되도록 하며 예외 상황이 발생할 경우 ArithmeticException 산술 연산 예외처리를 받아 0으로 나눗셈이 될 때에는 '0'이 출력되도록 한다.
4. 다음 소스 코드의 실행 결과는?
class ExceptionEx4 {
public static void main(String args[]) {
System.out.println(1);
System.out.println(2);
try {
System.out.println(3);
System.out.println(4);
} catch (Exception e) {
System.out.println(5);
} // try-catch의 끝
System.out.println(6);
} // main메서드의 끝
}
☞ 예외 상황이 발생하지 않으므로 5는 출력되지 않는다.
1
2
3
4
6
5. 다음 소스 코드의 실행 결과는 ?
class ExceptionEx5 {
public static void main(String args[]) {
System.out.println(1);
System.out.println(2);
try {
System.out.println(3);
System.out.println(0/0);
System.out.println(4);
} catch (ArithmeticException ae) {
System.out.println(5);
} // try-catch의 끝
System.out.println(6);
} // main메서드의 끝
}
☞ 예외 상황이 발생하여 System.out.println(0/0); 의 아래 4는 출력되지 않는다.
1
2
3
5
6
6. 다음 소스코드의 실행 결과는?
class ExceptionEx6 {
public static void main(String args[]) {
System.out.println(1);
System.out.println(2);
try {
System.out.println(3);
System.out.println(0/0);
System.out.println(4);
} catch (Exception e) { // ArithmeticException대신 Exception을 사용.
System.out.println(5);
} // try-catch의 끝
System.out.println(6);
} // main메서드의 끝
}
☞
1
2
3
5
6
7. 다음 소스 코드의 실행 결과는?
class ExceptionEx7 {
public static void main(String args[]) {
System.out.println(1);
System.out.println(2);
try {
System.out.println(3);
System.out.println(0/0);
System.out.println(4);
} catch (ArithmeticException ae) {
if (ae instanceof ArithmeticException)
System.out.println("true");
System.out.println("ArithmeticException");
} catch (Exception e) {
System.out.println("Exception");
} // try-catch의 끝
System.out.println(6);
} // main메서드의 끝
}
☞
1
2
3
true
ArithmeticException
6
8. 다음 ExceptionEx10 클래스는 컴파일시 에러가 나고, ExceptionEx11은 런타임시에 예외가 발생한다. 그 이유는 무엇인가?
class ExceptionEx10
{
public static void main(String[] args)
{
throw new Exception(); // Exception을 고의로 발생시킨다.
}
}
class ExceptionEx11
{
public static void main(String[] args)
{
throw new RuntimeException(); // RuntimeException을 고의로 발생시킨다.
}
}
☞ ExceptionEx10에서 throw new Exception으로 Exception을 고의로 발생시키고 있다. 때문에 try~catch문을 이용하여 예외를 처리해주어야 하는데, 이 예외 상황을 처리해줄 try~catch문이 없기 때문에 컴파일 시 에러가 발생하게 된다.
☞ ExceptionEx11에서 throw new RuntimeException을 고의로 발생시키고 있다. RuntimeException은 Exception과 다르게 unchecked 예외 상황이기 때문에 try~catch문을 사용하지 않더라도 발생한 예외 상황에 의해 컴파일 시 에러가 발생하지는 않는다.
9. 다음 소스코드의 공통된 부분을 걷어내 보자.
class FinallyTest
{
public static void main(String args[])
{
try
{
startInstall();
copyFiles();
deleteTempFiles();
}
catch (Exception e)
{
e.printStackTrace();
deleteTempFiles();
}
}
static void startInstall() {}
static void copyFiles() {}
static void deleteTempFiles() {}
}
☞ deleteTempFiles(); 가 공통으로 진행되고 있기 때문에 이 부분을 finally로 빼서 실행해준다.
class FinallyTest
{
public static void main(String args[])
{
try
{
startInstall();
copyFiles();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
deleteTempFiles();
}
}
static void startInstall() {}
static void copyFiles() {}
static void deleteTempFiles() {}
}
10. 다음 소스코드의 실행 결과를 적어보자.
class FinallyTest3
{
public static void main(String args[])
{
FinallyTest3.method1();
System.out.println("method1()의 수행을 마치고 main메서드로 돌아왔습니다.");
}
static void method1()
{
try
{
System.out.println("method1()이 호출되었습니다.");
return;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("method1()의 finally블럭이 실행되었습니다.");
}
}
}
☞
method1()이 호출되었습니다.
method1()의 finally블럭이 실행되었습니다.
method1()의 수행을 마치고 main메서드로 돌아왔습니다.
'Java > Day24' 카테고리의 다른 글
[Java] 이클립스 단축키 모음 (0) | 2021.12.05 |
---|---|
[Java] default 메소드 & static 메소드 (0) | 2021.12.05 |
[Java] Vector 클래스 (0) | 2021.12.05 |
[Java] import문 & static import문 (0) | 2021.12.05 |