try에 문제가 될수 있는 로직을 작성한다.
catch는 try에서 오류가 날 경우 실행한다.
catch는 if else if와 비슷하게 다중으로 사용할 수 있다.
catch는 if else if와 비슷하게 다중으로 사용할 수 있다.
finally는 작성하지 않아도 되고 무조건 실행한다.
catch(Exception e)
catch(ArrayIndexOutOfBoundsException e)
catch(ArithmeticException e)
catch(NumberFormatException e)
e.getMessage()
e.getString()
e.printStackTrace()
catch(Exception e)
catch(ArrayIndexOutOfBoundsException e)
catch(ArithmeticException e)
catch(NumberFormatException e)
e.getMessage()
e.getString()
e.printStackTrace()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class MyClass{
public static void main(String args[]){
int[] ary = {1,2,3};
// 오류가 발생 할 수 있는 로직을 try에 작성한다.
try{
System.out.println("#####");
System.out.println(ary[3]);
System.out.println("#####");
}
// try에서 오류가 있을 경우 catch를 실행한다.
catch(Exception e){
System.out.println(e.getMessage());
}
// fianlly는 작성하지 않아도 무방하다
// try와 catch 관계없이 finally는 무조건 실행한다.
finally{
System.out.println("무조건 실행");
}
}
}
| cs |
result:
#####
3 <- index out
무조건 실행
댓글
댓글 쓰기