1 package p1.exception;
2
3
4 /*
5 * 老师用电脑上课。
6 *
7 * 问题领域中涉及两个对象。
8 * 老师,电脑。
9 *
10 * 分析其中的问题。
11 * 比如电脑蓝屏。冒烟。
12 *
13 */
14
15 class LanPingException extends Exception{
16 public LanPingException(String msg) {
17 super(msg);
18 // TODO Auto-generated constructor stub
19 }
20 }
21 class MaoYanException extends Exception{
22 public MaoYanException(String msg) {
23 super(msg);
24 // TODO Auto-generated constructor stub
25 }
26 }
27 class NoPlanException extends Exception{
28 public NoPlanException(String msg) {
29 super(msg);
30 // TODO Auto-generated constructor stub
31 }
32 }
33 class Computer{
34 private int state = 2;
35
36 public void run() throws LanPingException,MaoYanException{
37 if (state == 1) {
38 throw new LanPingException("电脑蓝屏");
39 }
40 if (state == 2) {
41 throw new MaoYanException("电脑冒烟");
42 }
43 System.out.println("电脑运行");
44 }
45 public void reset() {
46 state = 0;
47 System.out.println("电脑重启");
48 }
49 }
50 class Teacher{
51 private String name;
52 private Computer comp;
53 public Teacher(String name) {
54 this.name = name;
55 comp = new Computer();
56 // TODO Auto-generated constructor stub
57 }
58 public void prelect() throws NoPlanException{
59 try {
60 comp.run();
61 System.out.println(name+"讲课");
62 } catch (LanPingException e) {
63 System.out.println(e.toString());
64 comp.reset();
65 prelect();
66
67 } catch (MaoYanException e) {
68
69 System.out.println(e.toString());
70 test();
71 //可以对电脑进行维修,
72 // throw e;//做练习但没有解决冒烟问题抛出给别人
73 throw new NoPlanException("课时进度无法完成,原因:"+e.getMessage());
74
75 }
76
77
78 }
79 public void test() {
80 System.out.println("大家练习");
81 }
82 }
83 public class ExceptionTest {
84
85 public static void main(String[] args) {
86 // TODO Auto-generated method stub
87 Teacher t = new Teacher("武老师");
88 try {
89 t.prelect();
90 } catch (NoPlanException e) {
91 System.out.println(e.toString()+"......");
92 System.out.println("换人");
93 }
94
95 }
96
97 }
98
99 /*
100 * class NoAddException extends Exception{
101 *
102 * }
103 *
104 *
105 * void addData(Data d) throws NoAddException{
106 *
107 * 连接数据库
108 * try{
109 * 添加数据。出现异常 SQLException();
110 * }catche (SQLException e) {
111 * //处理代码
112 *
113 * thorw new NoAddException(); 内部处理了异常对外进行异常的转换,也叫异常的封装
114 * 不该暴露出的问题就不暴露,你暴露了对方也不能处理
115 *
116 * }finally{
117 * 关闭数据库
118 * }
119 *
120 *
121 *
122 *
123 */
124 * }

ExceptionTest

java-异常-异常应用的更多相关文章

  1. java的异常总结

    异常:在java程序中也出现不正常的情况,这个就叫异常.java是面向对象的语言.任何事物都可以用类来描述,同样异常也是一种事物,java中提供了很多异常类 很多异常堆积起来叫做异常体系 Throwa ...

  2. java的异常

    下面是我对Java异常知识的几个小总结,也算是资源回收一下 一.Java异常的知识 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的.比如说,你的代码少了一个分号,那么运 ...

  3. 《java中异常和错误》

    异常和错误的区别. 异常: 在Java中程序的错误主要是语法错误和语义错误,一个程序在编译和运行时出现的错误我们统一称之为异常,它是VM(虚拟机)通知你的一种方式,通过这种方式,VM让你知道,你(开发 ...

  4. Atitit java的异常exception 结构Throwable类

    Atitit java的异常exception 结构Throwable类 1.1. Throwable类 2.StackTrace栈轨迹1 1.2. 3.cause因由1 1.3. 4.Suppres ...

  5. Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  6. java 异常

    异常简介 java中有Error和Exception Error:是程序无法处理的错误,表示运行应用程序中较严重问题.大多数错误与代码编写者执行操作无关,而表示运行时JVM出现的问题. Excepti ...

  7. Java基础——异常体系

    在Java中,异常对象都是派生于Throwable类的一个实例,Java的异常体系如下图所示: 所有的异常都是由Throwable继承而来,在下一层立即分解为两个分支,Error和Exception. ...

  8. Java并发编程:Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  9. 【转】Java ConcurrentModificationException异常原因和解决方法

    原文网址:http://www.cnblogs.com/dolphin0520/p/3933551.html Java ConcurrentModificationException异常原因和解决方法 ...

  10. JAVA基础——异常详解

    JAVA异常与异常处理详解 一.异常简介 什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域的情况,称之为异常. java中异常的体系是怎么样的呢? 1 ...

随机推荐

  1. IDEA通过git回滚到某个提交节点或某个版本

    1.项目右键后,点击"Git - Show History" 这里会显示有历史提交的版本记录(这里我们假设要回滚到 "提交" 版本中) 2.选中 "提 ...

  2. C语言之字符串替换库函数replace

    头文件 #include <algorithm> 例子 下面的代码, 将字符串中的 /替换为\ std::string str("C:/demo/log/head/send&qu ...

  3. Git统计代码变化率

    统计2017-03-01到2017-03-31代码变更率 代码统计命令参考:git log --pretty=tformat: --since ==2017-03-01 --until=2017-03 ...

  4. 【LeetCode】1024. Video Stitching 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  5. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  6. 【LeetCode】617. Merge Two Binary Trees 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  7. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  8. x86-1-32位x86 处理器编程架构

    x86(32位)-1-32位x86 处理器编程架构 Intel 32 位处理器架构简称IA-32(Intel Architecture,32-bit) x86是指intel的86系列的CPU统称,比如 ...

  9. 分布式链路追踪自从用了SkyWalking,睡得真香!

    本篇文章介绍链路追踪的另外一种解决方案Skywalking,文章目录如下: 什么是Skywalking? 上一篇文章介绍了分布式链路追踪的一种方式:Spring Cloud Sleuth+ZipKin ...

  10. golang 算法题 : 二维数组搜索值

    package mainimport "fmt"func main() { matrix := [][]int{ {1, 4, 7, 11, 15}, {2, 5, 8, 12, ...