while문 안에 try-catch 구문을 사용할 때
Exception이 발생해도 break를 만나기 전까지 while문이 실행된다는 이야기를 들었다...
놀라서 친구들한테 물어보니 나만 몰랐다.................
int num = 0;
while(true){
try {
System.out.println("==============");
System.out.println(++num);
if(num == 2) {
throw new Exception();
}else if(num == 3){
break;
}
}catch(Exception e) {
System.out.println("Exception 발생");
}finally{
System.out.println("finally");
}
}
출력 결과
==============
1
finally
==============
2
Exception 발생
finally
==============
3
finally
실습은 진행해보았지만 아직 어떨 때 사용해야하는 방식인지는 잘 모르겠다..
다른 사람들의 응용 예시를 보니 사용자에게 입력을 받았는데 유효한 값이 아닐 경우에 많이 사용하는 것 같다..