java的5种异常的语法

1 try{}catch(){}

​ 2 try{}catch(){}catch(){}

​ 3 try{}catch(){}finally{}

​ 4 try{}catch(){}catch(){}finally{}

​ 5 try{}finally{} : 只对异常进行捕获,没有对异常进行处理

  1 package com.lv.study.am.first;
  2 
  3 //try
  4 //catch
  5 //finally(异常处理:一定会运行)    final(修饰常量)
  6 public class DemoTryYF {
  7 
  8     public static void main(String[] args) {
  9 
 10         try {
 11             String str = null;
 12             str.getClass();
 13 
 14         } catch (Exception e) {
 15 
 16             System.out.println("2");
 17             //System.exit(0); // 结束程序;
 18 
 19         } finally { // 无论发生,代码块都会运行
 20             System.out.println("1");
 21         }
 22 
 23         System.out.println("3");
 24 
 25         
 26         System.out.println("+++++++++++++++++++++++++++++++");
 27         test4();
 28 
 29     }
 30 
 31     public static void test1() {
 32 
 33         System.out.println("1:");
 34 
 35         try {
 36 
 37             int i = 0;
 38             int j = 19 / i;
 39 
 40         } catch (Exception e) {
 41 
 42             System.out.println("对异常进行一些响应的纪录,处理... 然后在运行接下来的代码");
 43 
 44         }
 45 
 46         // try里面的局部变量,咱们在try外面不能用;
 47         // i++; //确认答案[和辅导老师确认一下答案]
 48 
 49         System.out.println("2");
 50 
 51     }
 52 
 53     public static void test2() {
 54 
 55         System.out.println("1:");
 56 
 57         try {
 58 
 59             int i = 0;
 60             int j = 19 / i;
 61 
 62         } catch (NullPointerException e) {
 63             System.out.println("对空指针异常进行处理");
 64         } catch (Exception e) {
 65             System.out.println("对异常进行一些响应的纪录,处理... 然后在运行接下来的代码");
 66         }
 67 
 68         System.out.println("2");
 69 
 70     }
 71 
 72     // 当方法涉及到返回值
 73     public static int test3() {
 74 
 75         System.out.println("1:");
 76 
 77         try {
 78 
 79             String str = null;
 80             System.out.println(str.getClass());
 81 
 82         } catch (NullPointerException e) {
 83             System.out.println("3 : 对空指针异常进行处理");
 84 
 85             // 当出现空指针异常的时候,我们直接返回-1;代表当前方法调用出错;
 86 
 87             return -1;// 代表方法调用出错 ; 直接对方法进行返回,方法后面的代码都不会执行了
 88 
 89         } catch (Exception e) {
 90             System.out.println("对异常进行一些响应的纪录,处理... 然后在运行接下来的代码");
 91 
 92         } finally { // 这个代码块里面,我们的代码不管你处理了什么异常,如何进行处理,都会运行
 93             System.out.println(" 2: 这句代码一定要运行");
 94         }
 95 
 96         System.out.println("4:这个不会执行");
 97 
 98         return 0;
 99     }
100 
101     //
102     public static void test4() {
103 
104         // 只有异常的捕获,没有对异常进行处理 // 但是我们有一些必要的代码可以运行
105 
106         System.out.println("1:");
107 
108         try {
109 
110             System.out.println("2:");
111             String str = null;
112             System.out.println(str.getClass());
113 
114         }
115         // catch (Exception e) {
116         //
117         // System.out.println("5:");
118         //
119         // }
120         finally {
121 
122             System.out.println("4:");
123         }
124 
125         System.out.println("3:");
126 
127     }
128 
129 }
原文地址:https://www.cnblogs.com/dabu/p/12458487.html