Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- Machine Learning
- 분류 결과표
- matplotlib
- 반복문
- analizer
- data
- DataAccess
- del
- string
- DataFrame
- sklearn
- len()
- wcss
- pandas
- numpy
- nan
- append()
- 최솟값
- elbow method
- dendrogram
- Dictionary
- IN
- count()
- 덴드로그램
- list
- Python
- function
- hierarchical_clustering
- insert()
- 최댓값
Archives
- Today
- Total
개발공부
[Java] try catch finally 문법 본문
Try-Catch-Finally
예외를 처리하기 위한 문법이다.
일단 Try문을 실행하고, catch에 있는 예외가 발생하면 catch문이 실행된다.
그리고 예외가 발생하든 안하든 finally 문은 반드시 실행된다.
예제
예외 X
import java.util.ArrayList;
public class TryMain {
public static void main(String[] args) {
// try catch finally 문법 : 예외를 처리하는 방법
// 예외가 발생했을 때, 내가 원하는 코드를 실행시키는 방법
int[] arr = {15, 2, 7};
try {
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i]);
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("배열 인덱스 예외 발생시 처리코드");
System.out.println(e.toString());
} catch(ArithmeticException e) {
System.out.println("연산 예외 발생시 처리하는 코드");
System.out.println(e.getMessage());
} catch(Exception e) {
System.out.println("나머지 모든 예외상황은 여기서 처리");
System.out.println(e.getMessage());
} finally {
System.out.println("예외가 발생하든 하지않든, 꼭 실행해야 하는 코드는 여기 작성");
}
}
}

ArrayIndexOutOfBoundsException
import java.util.ArrayList;
public class TryMain {
public static void main(String[] args) {
// try catch finally 문법 : 예외를 처리하는 방법
// 예외가 발생했을 때, 내가 원하는 코드를 실행시키는 방법
int[] arr = {15, 2, 7};
try {
for(int i=0; i<arr.length+1; i++) {
System.out.println(arr[i]);
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("배열 인덱스 예외 발생시 처리코드");
System.out.println(e.toString());
} catch(ArithmeticException e) {
System.out.println("연산 예외 발생시 처리하는 코드");
System.out.println(e.getMessage());
} catch(Exception e) {
System.out.println("나머지 모든 예외상황은 여기서 처리");
System.out.println(e.getMessage());
} finally {
System.out.println("예외가 발생하든 하지않든, 꼭 실행해야 하는 코드는 여기 작성");
}
}
}

ArithmeticException
import java.util.ArrayList;
public class TryMain {
public static void main(String[] args) {
try catch finally 문법 : 예외를 처리하는 방법
// 예외가 발생했을 때, 내가 원하는 코드를 실행시키는 방법
int k = 4/0;
System.out.println(k);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("배열 인덱스 예외 발생시 처리코드");
System.out.println(e.toString());
} catch(ArithmeticException e) {
System.out.println("연산 예외 발생시 처리하는 코드");
System.out.println(e.getMessage());
} catch(Exception e) {
System.out.println("나머지 모든 예외상황은 여기서 처리");
System.out.println(e.getMessage());
} finally {
System.out.println("예외가 발생하든 하지않든, 꼭 실행해야 하는 코드는 여기 작성");
}
}
}

그외 Exception
import java.util.ArrayList;
public class TryMain {
public static void main(String[] args) {
// try catch finally 문법 : 예외를 처리하는 방법
// 예외가 발생했을 때, 내가 원하는 코드를 실행시키는 방법
try {
ArrayList<String> myList = null;
myList.add("안녕?");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("배열 인덱스 예외 발생시 처리코드");
System.out.println(e.toString());
} catch(ArithmeticException e) {
System.out.println("연산 예외 발생시 처리하는 코드");
System.out.println(e.getMessage());
} catch(Exception e) {
System.out.println("나머지 모든 예외상황은 여기서 처리");
System.out.println(e.getMessage());
} finally {
System.out.println("예외가 발생하든 하지않든, 꼭 실행해야 하는 코드는 여기 작성");
}
}
}

'Java > Basic' 카테고리의 다른 글
| [Java] Foreach 루프 사용법 for(:) (0) | 2022.07.18 |
|---|---|
| [Java] HashMap 사용법 (0) | 2022.07.06 |
| [Java] ArrayList 사용법 (0) | 2022.07.06 |
| [Java] Interface 인터페이스 (0) | 2022.07.06 |
| [Java] Abstract Class 추상 클래스 (0) | 2022.07.06 |