Java异常抛出及try,catch应用实例

复制代码
 1 class lanpingException extends Exception
 2 {
 3     lanpingException(String msg)
 4     {
 5         super(msg);
 6     }
 7 }
 8 
 9 class maoyanException extends Exception
10 {
11     maoyanException(String msg)
12     {
13         super(msg);
14     }
15 }
16 
17 class Computer
18 {
19     private int state=2;
20     public void run()throws lanpingException,maoyanException
21     {
22         if(state==1)
23         {
24             throw new lanpingException("lanping!!!");
25         }
26         if(state==2)
27         {
28             throw new maoyanException("maoyan!!!");
29         }
30         System.out.println("run bat");
31     }
32     public void reset()
33     {
34         state=0;
35         System.out.println("computer reset!");
36     }
37 }
38 
39 class Teacher
40 {
41     private String name;
42     private Computer comp;
43     Teacher(String name)
44     {
45         this.name=name;
46         comp=new Computer();
47     }
48     public void prelect()throws maoyanException
49     {
50         try
51         {
52             comp.run();
53             System.out.println(name+" speak");
54         }
55         catch(lanpingException e)
56         {
57             System.out.println(e.toString());
58             comp.reset();
59             prelect();
60         }
61         catch(maoyanException e)
62         {
63             System.out.println(e.toString());
64             test();
65             throw e;
66         }
67     }
68     public void test()
69     {
70         System.out.println("test yourself!");
71     }
72 }
73 
74 class Kandra
75 {
76     public static void main(String[] args)
77     {
78         Teacher pp=new Teacher("cao");
79         try
80         {
81             pp.prelect();
82             
83         }
84         catch(maoyanException e)
85         {
86             System.out.println("......");
87         }
88     }
89 }
复制代码

 

posted @   Angel_Kitty  阅读(1489)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示
西雅图
14:14发布
西雅图
14:14发布
6°
西南风
5级
空气质量
相对湿度
93%
今天
中雨
3°/9°
周日
雨夹雪
3°/6°
周一
小雨
3°/10°