심도 분석: 자바 에서 어떻게 이상 한 지 한 편 으로 해결 해 드릴 게 요!
초 면 이상
우리 의 코드 에서 '이상' 을 접 했 습 니 다. 예 를 들 어:
나 누 기 0
System.out.println(10 / 0);
//
Exception in thread "main" java.lang.ArithmeticException: / by zero
배열 아래 표 시 는 경 계 를 넘 습 니 다.
int[] arr = {1, 2, 3};
System.out.println(arr[100]);
//
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
null 개체 방문
public class Test {
public int num = 10;
public static void main(String[] args) {
Test t = null;
System.out.println(t.num);
}
}
//
Exception in thread "main"java.lang.NullPointerException
이상 이란 프로그램 이 실 행 될 때 오류 가 발생 했 을 때 호출 자 에 게 알 리 는 메커니즘 을 말한다.
방어 프로 그래 밍
오 류 는 코드 에 객관 적 으로 존재 한다. 따라서 우 리 는 프로그램 에 문제 가 생 겼 을 때 즉시 프로그램 원숭이 에 게 알려 야 한다. 우 리 는 두 가지 주요 한 방식 이 있다.
LBYL: Look Before You Leap. 조작 하기 전에 충분 한 검 사 를 합 니 다. EAFP: It 's Easier to Ask Forgiveness than Permission. 먼저 조작 하고 문제 가 생기 면 처리 합 니 다.
이상 한 이점
예 를 들 어 우 리 는 위 코드 로 왕 의 영광 을 시작 하 는 과정 을 보 여 준다.
LBYL 스타일 코드 (이상 사용 하지 않 음)
boolean ret = false;
ret = ();
if (!ret) {
;
return;
}
ret = ();
if (!ret) {
;
return;
}
ret = ();
if (!ret) {
;
return;
}
ret = ();
if (!ret) {
;
return;
}
ret = ();
if (!ret) {
;
return;
}
EAFP 스타일 의 코드 (이상 사용)
try {
();
();
();
();
();
...
} catch ( ) {
;
} catch ( ) {
;
} catch ( ) {
;
} catch ( ) {
;
} catch ( ) {
;
}
두 가지 서로 다른 스타일 의 코드 를 비교 해 보면 우 리 는 첫 번 째 방식 을 사용 하고 정상 적 인 절차 와 오류 처리 프로 세 스 코드 가 혼 합 된 것 을 발견 할 수 있다. 두 번 째 방식 은 정상 적 인 절차 와 오류 프로 세 스 가 분리 되 어 코드 를 이해 하기 쉽다.
이상 한 기본 용법
캡 처 이상 기본 문법
try{
;
}[catch ( ) {
} ... ]
[finally {
}]
1. try 코드 블록 에 이상 이 발생 할 수 있 는 코드 를 넣 었 습 니 다. 2. catch 코드 블록 에 이상 이 발생 한 후의 처리 행위 가 놓 여 있 습 니 다. 3. finally 코드 블록 에 있 는 코드 는 사후 처리 에 사용 되 며 마지막 에 실 행 됩 니 다. 4. 그 중에서 catch 와 finally 는 상황 에 따라 추가 하거나 추가 하지 않 을 수 있 습 니 다. 코드 예제 1 이상 을 처리 하지 않 습 니 다.
int[] arr = {1, 2, 3};
System.out.println("before");
System.out.println(arr[100]);
System.out.println("after");
//
before
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
우 리 는 일단 이상 이 발생 하면 프로그램 이 종 료 됩 니 다. after 가 올 바 르 게 출력 되 지 않 았 습 니 다.
코드 예제 2 try catch 를 사용 한 프로그램 실행 과정
int[] arr = {1, 2, 3};
try {
System.out.println("before");
System.out.println(arr[100]);
System.out.println("after");
} catch (ArrayIndexOutOfBoundsException e) {
//
e.printStackTrace();
}
System.out.println("after try catch");
//
before
java.lang.ArrayIndexOutOfBoundsException: 100
at demo02.Test.main(Test.java:10)
after try catch
try 에 이상 이 생기 면 try 코드 블록 에 있 는 프로그램 은 계속 실행 되 지 않 고 catch 에 있 는 코드 에 맡 겨 집 니 다. catch 실행 이 끝나 면 계속 실 행 됩 니 다.
코드 예제 3 catch 는 해당 종류의 이상 만 처리 할 수 있 습 니 다. 코드 를 수정 하여 빈 포인터 이상 을 던 지게 했 습 니 다.
int[] arr = {1, 2, 3};
try {
System.out.println("before");
arr = null;
System.out.println(arr[100]);
System.out.println("after");
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
System.out.println("after try catch");
//
before
Exception in thread "main" java.lang.NullPointerException
at demo02.Test.main(Test.java:11)
이 때 catch 문 구 는 방금 빈 포인터 이상 을 포착 할 수 없습니다. 이상 유형 이 일치 하지 않 기 때 문 입 니 다.
코드 예시 4 catch 여러 개 가능
int[] arr = {1, 2, 3};
try {
System.out.println("before");
arr = null;
System.out.println(arr[100]);
System.out.println("after");
} catch (ArrayIndexOutOfBoundsExceptione) {
System.out.println(" ");
e.printStackTrace();
} catch (NullPointerException e) {
System.out.println(" ");
e.printStackTrace();
}
System.out.println("after try catch");
//
before
java.lang.NullPointerException
at demo02.Test.main(Test.java:12)
after try catch
한 단락 의 코드 는 여러 가지 이상 을 던 질 수 있 습 니 다. 다른 이상 은 서로 다른 처리 방식 이 있 습 니 다. 따라서 여러 개의 catch 코드 블록 을 조합 할 수 있 습 니 다. 여러 개의 이상 한 처리 방식 이 완전히 같다 면 이렇게 쓸 수도 있 습 니 다.
catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
...
}
코드 예제 5 도 하나의 catch 로 모든 이상 을 포착 할 수 있 습 니 다 (추천 하지 않 음)
int[] arr = {1, 2, 3};
try {
System.out.println("before");
arr = null;
System.out.println(arr[100]);
System.out.println("after");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("after try catch");
//
before
java.lang.NullPointerException
at demo02.Test.main(Test.java:12)
after try catch
코드 예제 6 finally 는 자원 방출 과 같은 마지막 수습 작업 을 표시 합 니 다.
int[] arr = {1, 2, 3};
try {
System.out.println("before");
arr = null;
System.out.println(arr[100]);
System.out.println("after");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("finally code");
}
//
before
java.lang.NullPointerException
at demo02.Test.main(Test.java:12)
finally code
이상 이 있 든 없 든 finally 의 코드 는 반드시 실 행 될 것 입 니 다. 최종 적 으로 Scanner 의 close 방법 이 실 행 될 것 을 보장 합 니 다.
코드 예제 7 try 를 사용 하여 자원 을 회수 하 는 것 을 책임 집 니 다. 방금 코드 는 등가 쓰기 방법 이 있 습 니 다. Scanner 대상 을 try 의 () 에서 만 들 면 try 가 실 행 된 후에 Scanner 의 close 방법 을 자동 으로 호출 할 수 있 습 니 다.
try (Scanner sc = new Scanner(System.in)) {
int num = sc.nextInt();
System.out.println("num = " + num);
} catch (Exception e) {
e.printStackTrace();
}
코드 예제 8 이 방법 에 이상 을 처리 하 는 방법 이 적당 하지 않 으 면 호출 스 택 을 따라 위로 전 달 됩 니 다.
public static void main(String[] args) {
try {
func();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
System.out.println("after try catch");
}
public static void func() {
int[] arr = {1, 2, 3};
System.out.println(arr[100]);
}
//
java.lang.ArrayIndexOutOfBoundsException: 100
at demo02.Test.func(Test.java:18)
at demo02.Test.main(Test.java:9)
after try catch
코드 예제 9. 위 로 계속 전달 해도 이상 을 처리 할 방법 이 없 으 면 JVM 에 맡 기 고 프로그램 이 이상 하 게 종 료 됩 니 다. (우리 가 처음에 try catch 를 사용 하지 않 았 을 때 와 같 습 니 다)
public static void main(String[] args) {
func();
System.out.println("after try catch");
}
public static void func() {
int[] arr = {1, 2, 3};
System.out.println(arr[100]);
}
//
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at demo02.Test.func(Test.java:14)
at demo02.Test.main(Test.java:8)
프로그램 이 이상 하 게 종료 되 었 습 니 다. System. out. println ("after try catch") 에 실행 되 지 않 았 습 니 다.이 분야
이상 처리 프로 세 스
프로그램 에서 try 의 코드 를 먼저 실행 합 니 다. try 의 코드 에 이상 이 생기 면 try 의 코드 를 끝 냅 니 다. catch 의 이상 유형 과 일치 하 는 지 확인 합 니 다. 일치 하 는 이상 유형 을 찾 으 면 catch 의 코드 를 실행 합 니 다. 일치 하 는 이상 유형 을 찾 지 못 하면 상위 호출 자 에 게 이상 을 전달 합 니 다. 일치 하 는 이상 유형 을 찾 든 말 든,finally 의 코드 는 모두 실 행 됩 니 다 (이 방법 이 끝나 기 전에 실 행 됩 니 다). 상위 호출 자 도 처리 한 이상 이 없 으 면 계속 위로 전 달 됩 니 다. main 방법 에 도 적합 한 코드 처리 이상 이 없 을 때 까지 JVM 에 맡 기 고 처리 합 니 다. 이 때 프로그램 이 이상 하 게 종 료 됩 니 다.
이상 을 던지다
자바 에 내 장 된 클래스 가 이상 을 던 지 는 것 을 제외 하고 프로그램 원숭이 도 수 동 으로 이상 을 던 질 수 있 습 니 다. throw 키 워드 를 사용 하여 이 작업 을 완성 할 수 있 습 니 다.
public static void main(String[] args) {
System.out.println(divide(10, 0));
}
public static int divide(int x, int y) {
if (y == 0) {
throw new ArithmeticException(" 0 ");
}
return x / y;
}
//
Exception in thread "main" java.lang.ArithmeticException: 0
at demo02.Test.divide(Test.java:14)
at demo02.Test.main(Test.java:9)
이 코드 에서 우 리 는 실제 상황 에 따라 필요 한 이상 을 던 질 수 있 습 니 다. 구조 이상 대상 과 동시에 설명 적 인 정 보 를 지정 할 수 있 습 니 다.
이상 설명
우 리 는 이상 을 처리 할 때 이 코드 에 어떤 이상 이 발생 할 수 있 는 지 알 고 싶 습 니 다. 우 리 는 throws 키 워드 를 사용 하여 던 질 수 있 는 이상 표시 식 을 방법 이 정의 하 는 위치 에 표시 할 수 있 습 니 다. 따라서 호출 자 에 게 이러한 이상 을 포착 하 는 것 에 주의 하 라 고 일 깨 워 줍 니 다.
public static int divide(int x, int y) throws ArithmeticException {
if (y == 0) {
throw new ArithmeticException(" 0 ");
}
return x / y;
}
finally 에 대한 주의사항
finally 의 코드 는 반드시 실 행 될 것 을 보증 합 니 다. 이것 도 약간의 번 거 로 움 을 가 져 올 것 입 니 다.
public static void main(String[] args) {
System.out.println(func());
}
public static int func() {
try {
return 10;
} finally {
return 20;
}
}
//
20
메모: finally 가 실행 하 는 시 기 는 방법 이 되 돌아 오기 전에 (try 나 catch 에서 return 이 있 으 면 이 return 전에 finally 를 실행 합 니 다) 입 니 다. 그러나 finally 에 return 문구 가 존재 한다 면 finally 의 return 을 실행 하여 try 에 있 는 return 을 실행 하지 않 습 니 다. 일반적으로 finally 에 return 을 쓰 는 것 을 권장 하지 않 습 니 다..
사용자 정의 이상 클래스
자바 에는 이미 풍부 한 이상 류 가 내장 되 어 있 지만, 우리 의 실제 장면 에 서 는 우리 가 이상 류 를 확장 하고, 우리 의 실제 상황 에 부합 되 는 이상 류 를 만들어 야 하 는 경우 가 있 을 수 있 습 니 다. 예 를 들 어, 사용자 로그 인 기능 을 실현 하 는 것 입 니 다.
class UserException extends Exception {
public UserException(String message) {
super(message);
}
}
class PasswordException extends Exception {
public PasswordException(String message) {
super(message);
}
}
public class TestDemo3 {
private static String userName = "admin";
private static String password = "123456";
public static void main(String[] args) {
try {
login("admin", "123456");
} catch (UserException userError) {
userError.printStackTrace();
} catch (PasswordException passwordError) {
passwordError.printStackTrace();
}
}
public static void login(String userName, String password) throws UserException, PasswordException {
if (!TestDemo3.userName.equals(userName)) {
throw new UserException(" ");
}
if (!TestDemo3.password.equals(password)) {
throw new PasswordException(" ");
}
System.out.println(" "); }
}
결론: 모 르 는 게 있 으 면 아래 에 댓 글 달 아 주세요. 좋아요 눌 러 주세요!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.