Python 官方文档:入门教程 => 点击学习
描述说明: public class TryCatchStu { 实例代码: public static void main(String[] args) { Sy
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基本用法的文章就介绍到这了,更多相关Java中try catch用法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java中trycatch处理异常示例
本文链接: https://www.lsjlt.com/news/159211.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0