Java 手动抛异常
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 手动抛异常的更多相关文章
- java通过抛异常来返回提示信息
结论:如果把通过抛异常的方式得到提示信息,可以使用java.lang.Throwable中的构造函数: public Throwable(String message) { fillInStackTr ...
- JAVA主动抛异常的几种方式及捕捉结果输出对比
测试代码: /** * 测试异常抛出及捕捉 */ @Test public void test() { try { this.testA(); } catch (Exception ex) { Sys ...
- Java throw:异常的抛出怎么回事
到目前为止,你只是获取了被Java运行时系统抛出的异常.然而,程序可以用throw语句抛出明确的异常.Throw语句的通常形式如下: throw ThrowableInstance;这里,Thr ...
- java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误
/** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...
- Android ADT插件更新后程序运行时抛出java.lang.VerifyError异常解决办法
当我把Eclipse中的 Android ADT插件从21.1.0更新到22.0.1之后,安装后运行程序抛出java.lang.VerifyError异常. 经过调查,终于找到了一个有效的解决办法: ...
- 千万别在Java类的static块里写会抛异常的代码!
public class Demo{ static{ // 模拟会抛异常的代码 throw new RuntimeException(); } } 如果你在Java类的static块里写这样会抛异常的 ...
- AOP拦截日志类,抛异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode
AOP的日志拦截类中,抛出异常: java.lang.IllegalStateException: It is illegal to call this method if the current r ...
- 遍历List时删除元素导致List抛出java.util.ConcurrentModificationException异常
1 public static void main(String[] args) { 2 List<String> list = new ArrayList<String>() ...
- Jetty+json-lib库抛异常的问题解决过程(java.lang.NoClassDefFoundError: net/sf/json/JSONObject)
一.之前抛异常是将json库改成了fastjson解决的,参见: http://www.cnblogs.com/gossip/p/5369670.html 异常信息: 二.解决步骤 ...
- 【JAVA】学习路径64-补充-编写一个会抛异常的方法
有一些方法,在调用的时候有可能会出错,所以我们使用这些方法的时候会使用try catch. 比如InputStream里面的read()方法等等,那么这些方法是怎么实现抛异常的效果的呢? 能抛异常的方 ...
随机推荐
- 清空elementui让计数器input-number的默认值
<el-form-item label="考试时长:" prop="testTimeLong"> <el-input-number style ...
- 【JS 逆向百例】有道翻译接口参数逆向
逆向目标 目标:有道翻译接口参数 主页:https://fanyi.youdao.com/ 接口:https://fanyi.youdao.com/translate_o?smartresult=di ...
- 强化学习从基础到进阶-常见问题和面试必知必答[8]:近端策略优化(proximal policy optimization,PPO)算法
强化学习从基础到进阶-常见问题和面试必知必答[8]:近端策略优化(proximal policy optimization,PPO)算法 1.核心词汇 同策略(on-policy):要学习的智能体和与 ...
- Python自动化办公--Pandas玩转Excel数据分析【二】
相关文章: Python自动化办公--Pandas玩转Excel[一] Python自动化办公--Pandas玩转Excel数据分析[三] python处理Excel实现自动化办公教学(含实战)[一] ...
- 【3】opencv_contrib 4.3.0库配置+opencv安装
相关文章: [1]windows下安装OpenCV(4.3)+VS2017安装+opencv_contrib4.3.0配置 [2]Visual Studio 2017同时配置OpenCV2.4 以及O ...
- MySQL 存储过程与函数(精简笔记)
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RD ...
- LyScript 通过PEB结构解析堆基址
LyScript中默认并没有提供获取进程堆基址的函数,不过却提供了获取PEB/TEB的函数,以PEB获取为例,可以调用dbg.get_peb_address(local_pid)用户传入当前进程的PI ...
- 在K8S中,节点故障驱逐pod过程时间怎么定义?
在Kubernetes中,节点故障驱逐Pod的过程涉及多个参数和组件的相互作用.以下是该过程的简要概述: 默认设置:在默认配置下,节点故障时,工作负载的调度周期约为6分钟. 关键参数: node-mo ...
- 【Docker内容大集合】Docker从认识到实践再到底层原理大汇总
前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量博客汇总https://blog.cs ...
- Postgresql-数据库无法停止,报错:pg_ctl server does not shut down
根据您的查询,pg_ctl server does not shut down(pg_ctl服务无法关闭)的原因可能有很多.以下是一些可能的解决方案和代码示例: (1)杀死所有与PostgreSQL相 ...