군침이 싹 도는 코딩
try catch finally 본문
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는 에러가 발생하든 안하든 무조건 실행되는것이며 있어도 되고 없어도 된다
'JAVA > Basic' 카테고리의 다른 글
java.lang.NullPointerException 에러 (0) | 2023.01.25 |
---|---|
ArrayList 를 활용한 예제 (1) | 2023.01.25 |
HashMap 과 함수 ( put, get, remove, clear ) (0) | 2023.01.25 |
ArrayList 와 함수 ( add, get, size, set, remove, clear, isEmpty ) (0) | 2023.01.25 |
문자열을 처리하는 문자열 함수 ( concat, length, substring, split, trim, toUpperCase, toLowerCase, indexOf, contains, compareTo, equals ) (0) | 2023.01.25 |