exception 异常

语法:

 1 public class DemoCclass4Exception {
 2     public static void main(String[] args) {
 3         //TODO 异常
 4         //catch 可以多个
 5 
 6        /* try{
 7 
 8         }catch(Exception e){
 9 
10         }finally {
11 
12         }*/
13 
14         int i = 0;
15         int j = 10;
16         //System.out.println(j/i);
17 
18         try {
19             System.out.println(j/i);
20         }catch (ArithmeticException e){
21             //System.out.println(e.getMessage());
22            //e.getStackTrace();
23             e.printStackTrace();
24         }finally {
25             System.out.println("最总执行的代码....");
26         }
27     }
28 }

 

异常情况:

  1 public class DemoClass4Exception1 {
  2     public static void main(String[] args){
  3         //TODO 常见异常,不管有没有异常,都需要加上try...catch...
  4 
  5         /*
  6         * 1. 除数为0的算术异常 java.lang.ArithmeticException 继承了:RuntimeException
  7         *    运行期异常
  8         * */
  9         /*int i = 0;
 10         int j = 10 / i;*/
 11 
 12         /*
 13         * 2.空指针异常 java.lang.NullPointerException 继承了: RuntimeException
 14         *   调用了一个为空(null)对象的成员属性和成员方法,就会发生异常
 15         *   同理:如果是static静态属性和方法,user12=null, user12调用也不会出错
 16         *   运行期异常
 17         * */
 18         /*User12 user12 = null;
 19         //System.out.println(user12.toString());
 20         System.out.println(user12.name12); //frank 访问静态属性,是类的属性,所有即便对象是null,也不关心*/
 21 
 22         /*
 23         * 3.索引越界 java.lang.ArrayIndexOutOfBoundsException 继承了: RuntimeException
 24         *   运行期异常
 25         * */
 26         int[] i = new int[3];
 27         i[0] = 10;
 28         i[1] = 20;
 29         i[2] = 30;
 30         //i[3] = 40; //错误
 31         //System.out.println(i[3]); //错误
 32 
 33         /*
 34         *4.字符串索引越界 java.lang.StringIndexOutOfBoundsException 继承了: RuntimeException
 35         *   运行期异常
 36         * */
 37         String str = "qwerty";
 38         System.out.println(str.charAt(0));
 39         //System.out.println(str.charAt(6)); //错误
 40 
 41         System.out.println(str.substring(6)); // 没有报错,尽管6索引不存在,单此方法索引超过长度6才会错误,所以有点区别
 42         //System.out.println(str.substring(7)); // 错误
 43 
 44         System.out.println("----------------");
 45         /*
 46         * 5.格式化异常 java.lang.NumberFormatException
 47         * */
 48         String str2 = "1234";
 49         //Integer i2 = (Integer) str2; //报错误,语法错误,应该用方法来转换
 50         Integer i3 = Integer.parseInt(str2);
 51         System.out.println(i3); //1234
 52 
 53         //异常情况
 54         String str22 = "a1234";
 55         //Integer i4 = Integer.parseInt(str22);
 56         //System.out.println(i4);
 57 
 58         System.out.println("-----------");
 59         /*
 60         * 6.类型转换错误  java.lang.ClassCastException
 61         * */
 62         Object obj = new User13();
 63         //Employee emp = (Employee)obj; //错误
 64         //解决方案:
 65    /*     if(obj instanceof Employee){
 66             Employee emp = (Employee)obj;
 67         }*/
 68 
 69         /*
 70         * 7. 跑出异常throws
 71         * */
 72 /*        DOWork doWork = new DOWork();
 73         int m = 10,n = 0;
 74         try{
 75             doWork.calcu(m, n);
 76         }catch (Exception e){
 77             e.printStackTrace();  //新的异常java.lang.Exception,而不是系统异常
 78         }*/
 79 
 80         System.out.println("--------自定义异常-----------");
 81         /*
 82         * 8. 自定义异常
 83         * */
 84         String acc = "admin";
 85         String pwd = "admin123";
 86 
 87         try {
 88             Login(acc, pwd);
 89         }catch (AccountException e){
 90             System.out.println("登陆失败,原因:"+ e.getMessage());
 91         }catch (PasswordException e2){
 92             System.out.println("登陆失败,原因:"+ e2.getMessage());
 93         }catch (LoginException e3){
 94             System.out.println("登陆失败,其他原因");
 95         }
 96 
 97     }
 98 
 99     public static void Login(String acc, String pwd){
100         if(!"admin".equals(acc)){
101             throw new AccountException("账号登陆错误,请检查.");
102         }
103         if(!"admin".equals(pwd)){
104             throw new PasswordException("密码登陆错误,请检查.");
105         }
106         System.out.println("登陆成功.");
107     }
108 }
109 
110 class User12{
111     public static String name12 = "frank";
112 }
113 
114 class User13{
115 
116 }
117 
118 class Employee{
119 
120 }
121 
122 class DOWork{
123     public void calcu(int i, int j) throws Exception {
124         try {
125             System.out.println(i/j);
126         }catch (ArithmeticException e){
127             throw  new Exception();
128         }
129     }
130 }
131 
132 /*
133 * 自定义异常 三个异常类
134 * */
135 
136 class AccountException extends LoginException{
137     public AccountException(String message) {
138         super(message);
139     }
140 }
141 
142 class PasswordException extends LoginException{
143     public PasswordException(String message) {
144         super(message);
145     }
146 }
147 
148 //TODO 自定义类
149 class LoginException extends RuntimeException{
150     public LoginException(String message) {
151         super(message);
152     }
153 }

 

posted @ 2025-10-15 17:55  字节虫  阅读(12)  评论(0)    收藏  举报