JAVA/Basic
try catch finally
mugoori
2023. 1. 25. 14:23
import java.util.ArrayList;
public class TryMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ArrayList<String> nameList = new ArrayList<>();
nameList.add("홍길동");
}
catch (Exception e) {
System.out.println("에러 발생 상황에서 처리할 코드");
System.out.println(e.toString());
}
// 아래 finally 는 있어도 되고 없어도 된다
finally {
System.out.println("에러가 발생하든 안하든 무조건 실행 시키고 싶은 코드를 여기에 넣는다");
}
}
}
# 디버깅을 위해 try 문을 쓸수있다
파이썬과는 다르게 except 가 아닌 catch이며
finally는 에러가 발생하든 안하든 무조건 실행되는것이며 있어도 되고 없어도 된다