반응형
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 |
Tags
- pycharm
- 웹 용어
- web 용어
- web
- web 개발
- 머신러닝
- 데이터전문기관
- CES 2O21 참가
- Keras
- 결합전문기관
- java역사
- postorder
- vscode
- tensorflow
- mglearn
- 자료구조
- broscoding
- web 사진
- C언어
- CES 2O21 참여
- discrete_scatter
- bccard
- inorder
- 재귀함수
- classification
- html
- paragraph
- KNeighborsClassifier
- cudnn
- 대이터
Archives
- Today
- Total
bro's coding
java.Exception Handling(예외 처리).throw.UserDefineException(사용자 정의 예외) 본문
[IT]/java
java.Exception Handling(예외 처리).throw.UserDefineException(사용자 정의 예외)
givemebro 2021. 3. 10. 11:36반응형
사용자 정의 예외( User Define Exception )
특정 서비스를 구현할 때, 별도의 Exception이 필요하다면 사용자 정의 예외를 직접 만들어 사용
> extends Exception, 즉 java.lang.Exception class를 상속받아 사용
//ex)
public class AgeException extends Exception{}
case1
package step2;
// 사용자 정의 예외
// 특정 예외 Exception class를 직접 정의 함
class AgeException extends Exception {
AgeException() {
// TODO Auto-generated constructor stub
super("나이 정보에 문제가 있습니다.");// 부모 Exception(String message)를 실행
}
// 생성자 오버로딩
AgeException(String message) {
super(message);// 부모 Exception(String message)를 실행
}
}
class MovieService {
public void enter(int age) throws AgeException {
if (age < 19) { // 미성년자이면 AgeException을 발생
throw new AgeException(age + "세이므로 성인영화 관람 불가");
}
System.out.println("즐거운 영화 감상하삼");
}
}
public class TestException2 {
public static void main(String[] args) {
MovieService service = new MovieService();
int age = 15;// AgeException 발생 생황
// age = 22; // 정상 수행
try {
service.enter(age);
} catch (AgeException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println(e.getMessage());
}
System.out.println("정상 수행");
}
}
/*
15세이므로 성인영화 관람 불가
정상 수행
*/
case2
package step2;
public class TestThrows3 {
public static void main(String[] args) {
MemberService service = new MemberService();
String info = null;
try {
service.register(info);
} catch (InformationException e) {
System.out.println(e.getMessage());
}
}
}
package step2;
public class InformationException extends Exception {
public InformationException() {
// TODO Auto-generated constructor stub
super("잘못된 정보입니다.");
}
public InformationException(String message) {
super(message);
}
}
package step2;
public class MemberService {
public void register(String info) throws InformationException {
if (info == null) {
throw new InformationException("등록하려는 정보가 null이라 DB에 저장 불가");
} else if (info.equals("")) {
throw new InformationException("등록하려는 정보가 빈공란이라 DB에 저장 불가");
}
System.out.println(info + "DB에 저장");
}
}
case3
package step2;
// Exception 관련 주요 키워드(try, catch, finally, throw, throws)를 모두 사용
class DayException extends Exception {
private static final long serialVersionUID = 4933625584702780171L;
public DayException() {
super("Day관련 예외 발생");
}
public DayException(String message) {
// TODO Auto-generated constructor stub
super(message);
}
}
class DateService {
public void register(int day) throws DayException {
if (day < 1 || day > 31) {
throw new DayException(day + "는 Day의 범위에서 벗어납니다.");
}
System.out.println(day + "일이 등록되었습니다.");
}
}
public class TestThrows4 {
public static void main(String[] args) {
DateService service = new DateService();
int day = 32; // day:1~31
try {
service.register(day);
} catch (DayException d) {
System.out.println(d.getMessage());
}
}
}
case4
package step2;
// Exception 관련 주요 키워드(try, catch, finally, throw, throws)를 모두 사용
class DayException extends Exception {
private static final long serialVersionUID = 4933625584702780171L;
public DayException() {
super("Day관련 예외 발생");
}
public DayException(String message) {
// TODO Auto-generated constructor stub
super(message);
}
}
class DateService {
public void register(int day) throws DayException {
try {
if (day < 1 || day > 31) {
throw new DayException(day + "는 Day의 범위에서 벗어납니다.");
}
System.out.println(day + "일이 등록되었습니다.");
System.out.println("d");
} finally {
System.out.println("e");
}
System.out.println("f");
}
}
public class TestThrows4 {
public static void main(String[] args) {
DateService service = new DateService();
int day = 31; // day:1~31
// day = 32;
try {
service.register(day);
System.out.println("a");
} catch (DayException d) {
System.out.println(d.getMessage());
System.out.println("b");
}
System.out.println("c");
}
}
// exception 발생 : e > b > c
// exception미발생 : d > e > f > a > c
java.Exception Handling(예외 처리).throw
throw Exception을 고의로 발생 //ex) public void test() throws XXException{ if(특정 예외상황){ throw new XXException(); } }
broscoding.tistory.com
반응형
'[IT] > java' 카테고리의 다른 글
java.label (0) | 2021.03.11 |
---|---|
java.IO(입출력) (0) | 2021.03.11 |
java.String/StringBuilder (0) | 2021.03.10 |
java.Exception Handling(예외 처리).Unchecked Exception (0) | 2021.03.10 |
java.Exception Handling(예외 처리).throw (0) | 2021.03.10 |
java.Exception Handling(예외 처리).throws (0) | 2021.03.10 |
java.Exception Handling(예외 처리).try/catch (0) | 2021.03.09 |
java.Exception Handling(예외 처리).finally (0) | 2021.03.09 |
Comments