티스토리 뷰
반응형
Java 상위로 예외 넘기기 throws
throws : 호출한 상위 메소드로 예외처리 넘기기, 메소드 선언부에 작성
A메소드에서 B메소드를 호출시 예외가 발생했을 경우 B메소드 자체적으로 try-catch를 통해 처리 할 수 있지만
B메소드를 여러곳에서 사용한다고 할 때에는 호출하는 여러 메소드 마다 필요한 예외가 각각 다를수 도 있기에
예외처리를 구현할 기회를 줄 수 있다.
throws
사용하여 호출한 메소드로 예외 책임을 전가하기
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
|
public class ThrowsTest {
public static void main(String[] args){
try {
returnThrows(2);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Class Catch");
System.out.println(e.toString());
} catch (NullPointerException e) {
System.out.println("Null Catch");
System.out.println(e.toString());
} catch (NumberFormatException e) {
System.out.println("Number Catch");
System.out.println(e.toString());
}
}
public static void returnThrows(int a) throws ClassNotFoundException,NullPointerException,NumberFormatException{
if(a == 1) {
throw new ClassNotFoundException();
}else if(a == 2) {
throw new NullPointerException();
}else {
throw new NumberFormatException();
}
}
}
|
cs |
try ~ catch
해당 메소드에서 예외 처리하기
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
|
public class ThrowsTest {
public static void main(String[] args){
returnThrows(3);
}
public static void returnThrows(int a){
try {
if(a == 1) {
throw new ClassNotFoundException();
}else if(a == 2) {
throw new NullPointerException();
}else {
throw new NumberFormatException();
}
}catch(ClassNotFoundException e) {
System.out.println("기존 메소드 Class e");
System.out.println(e.toString());
}catch(NullPointerException e) {
System.out.println("기존 메소드 Null e");
System.out.println(e.toString());
}catch(NumberFormatException e) {
System.out.println("기존 메소드 Number e");
System.out.println(e.toString());
}
}
}
|
cs |

반응형
'Java' 카테고리의 다른 글
Enum equals , == (0) | 2024.04.11 |
---|---|
Mac Java 버전 관리 (0) | 2023.03.13 |
Windows OpenJdk 다운로드 및 설치 (0) | 2022.10.21 |
Java HMAC 암호화 (2) | 2021.12.15 |
List removeAll(Collection<?> c) (0) | 2021.12.03 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- actuator prometheus grafana
- Enum equals
- cursor ai 프로젝트
- 배치 관리 ui
- JdbcBatchItem
- cursor ai crud
- no `meta.properties` found in
- Spring boot Actuator
- CompositeItemWriter
- spring security
- cursor ai 프롬프트
- spring boot jpa
- tomcat gzip
- SpringBatch 5.1.1
- kafka oubox
- cursor ai
- cursorAI
- kafka srping event
- Spring Actuator
- spring boot jpa crud
- kafka redis
- quartz 실무 적용
- oubox pattern
- quartz spring batch
- 배치 모니터링
- Enum Equals ==
- Enum ==
- custom Item writer
- 잡 스케줄링
- 스케줄링 시스템
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함