>  기사  >  Java  >  Java 예외 잡기 설명

Java 예외 잡기 설명

PHP中文网
PHP中文网원래의
2017-06-20 09:58:091421검색

오늘은 try-catch-finally 문을 배웠는데, 꽤 간단하고 이해하기 쉬웠어요. 관련 유형의 질문을 검색했습니다. 그 결과 자신감이 찬물을 끼얹었다. 먼저 질문에 표시하고 신선한 공기를 마시러 나가십시오.

 1 public class TestEx { 2     private int c; 3  4     public TestEx() { 5     } 6  7     @SuppressWarnings("finally") 8     boolean testEx() throws Exception { 9         boolean ret = true;10         try {11             ret = testEx1();12         } catch (Exception e) {13             System.out.println("testEx, catch exception");14             ret = false;15             throw e;16         } finally {17             System.out.println("testEx, finally; return value=" + ret);18             return ret;19         }20     }21 22     @SuppressWarnings("finally")23     boolean testEx1() throws Exception {24         boolean ret = true;25         try {26             ret = testEx2();27             if (!ret) {28                 return false;29             }30             System.out.println("testEx1, at the end of try");31             return ret;32         } catch (Exception e) {33             System.out.println("testEx1, catch exception");34             ret = false;35             throw e;36         } finally {37             System.out.println("testEx1, finally; return value=" + ret);38             return ret;39         }40     }41 42     @SuppressWarnings("finally")43     boolean testEx2() throws Exception {44         boolean ret = true;45         try {46             int b = 12;47             for (int i = 2; i >= -2; i--) {48                 setC(b / i);49                 System.out.println("i=" + i);50             }51             return true;52         } catch (Exception e) {53             System.out.println("testEx2, catch exception");54             ret = false;55             throw e;56         } finally {57             System.out.println("testEx2, finally; return value=" + ret);58             return ret;59         }60     }61 62     public static void main(String[] args) {63         TestEx testException1 = new TestEx();64         try {65             testException1.testEx();66         } catch (Exception e) {67             e.printStackTrace();68         }69     }70 71     public int getC() {72         return c;73     }74 75     public void setC(int c) {76         this.c = c;77     }78 }

출력:

읽고 나서 생각했습니다.

1. Java 예외 처리 메커니즘을 내가 정말 이해하고 있는 걸까?

2. 자바 예외 처리, 정말 마스터했나요?

3. 캐치 본문에서 반환을 처리하는 방법은 무엇입니까?

4.드디어 몸에 복귀가 된다면 어떻게 해야 할까요?

5. 캐치에서 return이 동시에 발생하면 어떻게 해야 하나요?

6. 또 다른 System.exit()가 있나요? 마주치면 어떻게 해야 할까요? ?

7. 던지는 것만으로는 완전한 해결책이 아니며 계속해서 깊이 이해해야 합니다.

위 내용은 Java 예외 잡기 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.