这篇文章主要介绍Java中try catch如何处理异常,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完! 描述说明:public class TryCatchStu { 实例代码:&nbs
这篇文章主要介绍Java中try catch如何处理异常,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
public class TryCatchStu {
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("爱啦啦阿拉"); }}
以上是“Java中try catch如何处理异常”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!
--结束END--
本文标题: Java中try catch如何处理异常
本文链接: https://www.lsjlt.com/news/301313.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0