ホームページ  >  記事  >  Java  >  Java例外キャッチの説明

Java例外キャッチの説明

PHP中文网
PHP中文网オリジナル
2017-06-20 09:58:091420ブラウズ

今日、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. Java 例外処理、本当にマスターできたでしょうか?

3.キャッチ本体でのリターンの対処方法は?

4.最後に、体に戻りがあった場合はどうすればよいですか?

5. キャッチとファイナルで同時にリターンが発生した場合はどうすればよいですか?

6. 別の System.exit() はありますか?遭遇したらどうすればいいですか? ?

7. スローを知るだけでは完全な解決策ではなく、深く理解し続ける必要があります。

以上がJava例外キャッチの説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。