Java中try catch处理异常示例
作者:java时光 时间:2023-11-07 00:43:17
描述说明:
public class TryCatchStu {
/*try catch:自己处理异常
*try{
* 可能出现异常的代码
*}catch(异常类名A e){
* 如果出现了异常类A类型的异常,那么执行该代码
*}...(catch可以有多个)
*finally{
* 最终肯定必须要执行的代码(例如释放资源的代码)
*}
*代码执行的顺序:
*1.try内的代码从出现异常的那一行开始,中断执行
*2.执行对应的catch块内的代码
*3.继续执行try catch 结构之后的代码
*注意点:
*1.如果catch内的异常类存在子父类的关系,那么子类应该在前,父类在后
*2.如果finally中有return语句,那么最后返回的结果肯定以finally中的返回值为准
*3.如果finally语句中有return,那么没有被处理的异常将会被吞掉
*重写的注意点:
*1.儿子不能比父亲的本事大
*2.儿子要比父亲开放
*3.儿子不能比父亲惹更大的麻烦(子类的异常的类型不能是父类的异常的父类型)
*异常类Api:
*1.getMessage():获取异常描述信息字符串
*2.toString():返回异常类的包路径和类名和异常描述信息字符串
*3.printStackTrace():除了打印toString的信息外,还要打印堆栈信息
*/
实例代码:
public static void main(String[] args)
{
System.out.println(demo());
}
public static int demo(){
GirlHome gh = new GirlHome("小陈陈",'男',28);
try{
gh.intoHome();
System.out.println("进入了女生宿舍");
return 0;
}catch(SexOutException e){
//System.out.println("出现了异常");
//System.out.println(e);
e.demo();
e.printStackTrace();
}catch (AgeOutException e) {
// TODO: handle exception
}finally{
System.out.println("我必须被执行");
}
return 1;
//System.out.println("愉快的一天结束啦");
}
class GirlHome {
public String name;
public char sex;
public int age;
public GirlHome(String name,char sex,int age)
{
super();
this.name = name;
this.sex = sex;
this.age = age;
}
//如果发现进入者是男的,那么抛出性别异常
public void intoHome() throws SexOutException,AgeOutException{
if(sex!='女'){
SexOutException se = new SexOutException(name+"你不是女的,滚!!!");
throw se;
}
if(age>25){
throw new AgeOutException(name+"你的年龄太大了,滚!!!!");
}
System.out.println(name+"开心的进入了宿舍");
}
public void demo()throws Exception{}
}
class SmallGirlHome extends GirlHome{
public SmallGirlHome(String name, char sex, int age)
{
super(name, sex, age);
// TODO Auto-generated constructor stub
}
@Override
public void intoHome() throws SexOutException,AgeOutException
{
}
public void demo()
{
}
class AgeOutException extends Exception{
public AgeOutException(String message){
super(message);
}
}
class SexOutException extends Exception{
public SexOutException(String message){
super(message);
}
public void demo(){
System.out.println("爱啦啦阿拉");
}
}
来源:https://blog.csdn.net/wanghuiwei888/article/details/78818203
标签:java,try,catch
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#浅拷贝和深拷贝实例解析
2022-09-03 02:31:05
![](https://img.aspxhome.com/file/2023/5/100135_0s.png)
Android编程自定义对话框(Dialog)位置及大小的方法
2021-10-01 19:01:51
Unity计时器功能实现示例
2022-03-08 20:02:39
![](https://img.aspxhome.com/file/2023/8/88408_0s.gif)
Spring中的事务隔离级别和传播行为
2022-07-21 18:18:24
![](https://img.aspxhome.com/file/2023/5/86775_0s.png)
java垃圾回收原理之GC算法基础
2023-10-05 16:10:09
![](https://img.aspxhome.com/file/2023/2/87352_0s.png)
java 学习笔记(入门篇)_多选择结构switch语句
2023-02-04 11:35:59
IntellJ IDEA神器使用技巧(小结)
2023-08-08 22:01:44
使用JPA双向多对多关联关系@ManyToMany
2021-12-04 03:37:12
![](https://img.aspxhome.com/file/2023/8/129118_0s.png)
C语言实现线性表的基本操作详解
2022-02-24 22:08:32
![](https://img.aspxhome.com/file/2023/4/120794_0s.png)
java实现图书馆管理系统
2023-12-10 15:50:29
Java Zookeeper分布式分片算法超详细讲解流程
2023-07-08 06:56:36
SpringBoot ApplicationContext接口深入分析
2021-10-21 23:00:03
![](https://img.aspxhome.com/file/2023/6/61056_0s.png)
详解Java8新特性Stream之list转map及问题解决
2021-06-12 05:45:06
![](https://img.aspxhome.com/file/2023/3/100363_0s.png)
详解Android开发中Fragment的使用
2023-05-02 20:32:25
![](https://img.aspxhome.com/file/2023/7/68027_0s.png)
解决mybatis-generator生成器添加类注释方法无效的问题
2023-12-05 06:34:57
C#如何将Access中以时间段条件查询的数据添加到ListView中
2022-02-22 22:15:18
Android 使用viewpager实现无限循环(定时+手动)
2023-12-09 07:43:56
Java去掉小数点后面无效0的方案与建议
2023-11-29 11:46:57
Java基础知识之StringWriter流的使用
2023-01-19 00:47:52
Android Notification使用方法详解
2023-12-14 13:03:17