ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java 手动抛异常

2021-10-10 09:00:07  阅读:117  来源: 互联网

标签:Java 手动 System try println catch 异常 out


  1 package com.bytezero.throwable;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.io.IOException;
  7 
  8 import org.junit.Test;
  9 
 10 /**
 11  * 
 12  * @Description   异常处理 :
 13  * @author Bytezero·zhenglei!        Email:420498246@qq.com
 14  * @version
 15  * @date 上午8:35:40
 16  * @     一:异常处理 : 抓抛模型
 17  * 
 18  *  过程一: “抛”:程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成
 19  *  一个对应异常类的对象。并将此对象输出。
 20  *   一旦抛出对象以后,其后的代码不再执行。
 21  *   
 22  *   关于异常对象的产生:① 系统自动生成的异常对象
 23  *                      ② 手动生成一个异常对象,并抛出 throw。
 24  *   
 25  *   
 26  *   
 27  * 
 28  *   过程二:“抓”:可以理解为 异常的处理方式,:①try-catch-finally  ②throws
 29  *   
 30  *   
 31  *   二:try-catch-finally的使用
 32  *   
 33  *   try{
 34  *   
 35  *           //可能出现异常的代码
 36  *   
 37  *   }catch(异常类型1 变量名1){
 38  *   
 39  *       //处理异常的方式1
 40  *   
 41  *   }catch(异常类型2 变量名2){
 42  *   
 43  *       //处理异常的方式2
 44  *   
 45  *   }catch(异常类型3 变量名3){
 46  *   
 47  *       //处理异常的方式3
 48  *   
 49  *   }
 50  *   .....
 51  *   finally{
 52  *   
 53  *           //一定会执行的代码
 54  *   }
 55  *   
 56  *   说明:
 57  *   1.finally是可选的。
 58  *   2.使用try将可能出现的代码包装起来,在执行过程中,一旦出现异常,就会生成
 59  *   一个对应异常的对象,根据此对象的类型,去catch中进行匹配
 60  *   3.一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常的处理。一旦
 61  *   处理完成后,一旦处理完成,就跳出当前的try  catc 结构(在没有写finally的情况)。继续
 62  *   执行其后的代码
 63  *   4.catch中的异常类型 如果没有子父类关系,则说声明在上,都可以
 64  *     catch中的异常类型 如果满足子父类关系,则要求子类一定声明在父类的上面,否则,报错
 65  *
 66  *   5.常用的异常对象处理方式:①String getMessage()  
 67  *                            ②printStackTrace()
 68  * 
 69  *   6.在try结构中声明的变量,再出了try结构以后,就不能再被调用
 70  *   
 71  *   7.try - catch-finally结构可以相互嵌套。
 72  *   
 73  *   
 74  *   体会1:使用try-catch-finally处理编译异常时,使得程序在编译时就不在报错,但是运行时仍可能
 75  *   报错,相当于我们使用try - catch -finally 将一个编译时可能出现的异常,延迟到运行时出现。
 76  *
 77  *
 78  *   体会2:开发中,由于运行时异常比较常见,所有我们通常就不针对运行时异常编写try-catch-finally了,
 79  *   针对于编译时异常,我们说一定要考虑异常的处理。
 80  *  
 81  */
 82 public class ExceptionTest2 {
 83     
 84     
 85     @Test
 86     public void test2() {
 87         
 88         try {
 89             
 90                 File file = new File("hello.txt");
 91                 FileInputStream fis = new FileInputStream(file);
 92                 
 93                 int data = fis.read();
 94                 while(data != -1) {
 95                     System.out.println((char)data);
 96                     data = fis.read();
 97                     
 98                 }
 99                 fis.close();
100         }catch(FileNotFoundException e){
101             
102             e.printStackTrace();
103             
104         }catch(IOException e) {
105             e.printStackTrace();
106         }
107         
108     }
109     
110     
111     @Test
112     public void test1() {
113         
114         String str = "123"; 
115         str = "abc";
116         int num = 0;
117         try {
118              Integer.parseInt(str);
119             
120             System.out.println("Hello----------1");
121         
122         }catch(NullPointerException e) {
123             System.out.println("出现了空指针异常");
124             
125         }catch(NumberFormatException e) {
126             
127 //            System.out.println("出现 数值转换 异常了");
128             //String getMessage():
129             //System.out.println(e.getMessage());
130             //printStackTrace
131             e.printStackTrace(); 
132             
133         }catch(Exception e) {
134             
135             System.out.println("出现了 异常 ");
136         }
137         
138         //System.out.println(num);  //不可以调用,因为出了 try 结构
139         
140         System.out.println(num);
141         
142         System.out.println("Hello----------2");
143         
144     }
145     
146     
147     
148     
149 
150 }
 1 package com.bytezero.throwable;
 2 
 3 public class StudentsTest {
 4 
 5     public static void main(String[] args) {
 6         try {
 7             Student s = new Student();
 8             s.regist(-1001);
 9             System.out.println(s);
10         }catch(Exception e){
11 //            e.printStackTrace();
12             System.out.println(e.getMessage());
13         }
14     }
15 }
16 
17 class Student{
18     
19     int id;
20     
21     public void regist(int id) throws Exception {
22         
23         if(id > 0) {
24             
25             this.id = id;
26         }else {
27             //System.out.println("您输入的数据非法!");
28             
29             //手动抛出异常
30             //throw new RuntimeException("您输入的数据非法!");
31             throw new Exception("您输入的数据非法!");
32             //throw new MyException("数据非法-----自定义异常类"); 
33         }
34         
35     }
36 
37     @Override
38     public String toString() {
39         return "Student [id=" + id + "]";
40     }
41     
42     
43     
44     
45 }

 

标签:Java,手动,System,try,println,catch,异常,out
来源: https://www.cnblogs.com/Bytezero/p/15388541.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有