java异常处理的简单练习
作者:jingxian 时间:2023-12-01 02:20:48
异常的练习:
老师用电脑上课。
开始思考上课中出现的问题。
比如问题是
电脑蓝屏。
电脑冒烟。
要对问题进行描述,封装成对象。
可是当冒烟发生后,出现讲课进度无法继续。
出现了讲师的问题:课时计划无法完成。
class Teacher
{
private Computer cmp;
public void shangKe()throws NoPlanException /*声明异常*/
{
cmp=new Computer();
try
{
cmp.run();
}
catch(LanPingException e) /*电脑捕获处理蓝屏的异常*/
{
cmp.recst();
}
catch(MaoYanException e) /*电脑捕获处理电脑冒烟的异常*/
{
throw new NoPlanException("上课无法继续,因为"+e.getMessage()); /*电脑无法处理这个异常,继续把这个异常抛给老师来处理*/
}
System.out.println("老师上课"); /*没有异常,老师就正常上课*/
}
}
class LanPingException extends Exception /*自定义蓝屏异常*/
{
LanPingException(String m)
{
super(m);
}
}
class MaoYanException extends Exception /*自定义电脑冒烟异常*/
{
MaoYanException(String m)
{
super(m);
}
}
class NoPlanException extends Exception /*自定义老师处理异常*/
{
NoPlanException(String m)
{
super(m);
}
}
class Computer
{
private int state=3; /*不同的异常状态选择*/
public void run()throws LanPingException,MaoYanException
{
if(state==2)
{
throw new LanPingException("电脑蓝屏了"); /*符合条件就抛出异常对象*/
}
if(state==3)
{
throw new MaoYanException("电脑冒烟了");
}
System.out.println("电脑运行");
}
public void recst()
{
System.out.println("电脑重启");
}
}
class ExceptionText
{
public static void main(String args[])
{
Teacher t=new Teacher();
try
{
t.shangKe();
}
catch(NoPlanException e) /*老师捕获处理电脑冒烟异常*/
{
System.out.println(e.toString());
}
}
}
运行结果:
NoPlanException: 上课无法继续,因为电脑冒烟了
标签:java,异常处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java的主要特性学习总结
2021-11-16 05:46:19
详解Java拦截器以及自定义注解的使用
2023-05-14 01:40:16
Spring-data-redis操作redis cluster的示例代码
2022-12-17 17:04:00
Spring中IOC和AOP的深入讲解
2023-11-19 10:20:59
![](https://img.aspxhome.com/file/2023/9/59529_0s.jpg)
mybatis使用collection嵌套查询的实现
2021-07-06 22:04:58
![](https://img.aspxhome.com/file/2023/1/71861_0s.png)
初识MyBatis及基本配置和执行
2021-11-12 05:53:40
![](https://img.aspxhome.com/file/2023/7/75487_0s.jpg)
详解Spring MVC CORS 跨域
2023-11-25 08:04:37
Mybatis步骤分解实现一个增删改查程序
2021-09-16 01:12:43
![](https://img.aspxhome.com/file/2023/0/65730_0s.png)
C#仿QQ实现简单的截图功能
2021-10-11 16:43:15
![](https://img.aspxhome.com/file/2023/6/116426_0s.gif)
Spring Boot统一异常处理最佳实践(拓展篇)
2023-10-29 16:00:04
![](https://img.aspxhome.com/file/2023/6/61126_0s.png)
webuploader 实现图片批量上传功能附实例代码
2022-12-03 09:13:34
![](https://img.aspxhome.com/file/2023/8/67148_0s.png)
使用Java Minio搭建自己的文件系统详解
2023-05-20 05:15:01
![](https://img.aspxhome.com/file/2023/7/130907_0s.jpg)
Python调用Java可执行jar包问题
2021-06-02 10:26:26
![](https://img.aspxhome.com/file/2023/9/102899_0s.png)
Java ArrayList类的基础使用讲解
2021-11-14 10:22:18
![](https://img.aspxhome.com/file/2023/4/60914_0s.png)
Java实现简单文件过滤器功能
2021-09-13 23:27:58
![](https://img.aspxhome.com/file/2023/1/62111_0s.png)
SpringBoot web静态资源配置详解
2023-09-24 14:13:26
![](https://img.aspxhome.com/file/2023/0/106090_0s.png)
解析C#中@符号的几种使用方法详解
2022-11-09 03:26:03
java利用udp实现发送数据
2023-03-21 18:30:49
![](https://img.aspxhome.com/file/2023/4/69734_0s.jpg)
Mybatis模糊查询及自动映射实现详解
2021-10-29 12:05:40
Java Bean 作用域及它的几种类型介绍
2022-12-02 20:39:42
![](https://img.aspxhome.com/file/2023/9/75479_0s.png)