GirlFriendNotFoundException异常是怎样处理的?

GirlFriendNotFoundException异常是怎样处理的?


如果说要去创造这个异常,那么我们的JAVA程序里,肯定是继承Exception去处理,所有我们可以先实现一个自己的Exception

/**
 * GirlFriendNotFoundException
 * 
 * @author LGL
 *
 */
class GirlFriendNotFoundException extends Exception {

	private String msg;

	public GirlFriendNotFoundException(String msg) {
		this.msg = msg;
	}

	@Override
	public String getMessage() {
		// TODO Auto-generated method stub
		return msg;
	}
}

这里只是重写了getMessage方法,反馈我们的异常信息,那我们来定义一个方法

class Demo {
	int dev(int age) throws GirlFriendNotFoundException {
		if (a < 18) {
			// 手动通过throw关键字抛出自定义异常对象
			throw new GirlFriendNotFoundException("Girl Friend Not Found !!!");
		}
		return age;
	}
}

我们可以传递一个参数age,如果女朋友的年龄小于18就抛出异常,那我们来测试一下

/**
 * 自定义异常
 * 
 * @author LGL
 *
 */
public class CustomException {
	public static void main(String[] str) {
		Demo d = new Demo();
		try {
			d.dev(16);
		} catch (GirlFriendNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

哈哈,我们女朋友还只有16岁,那我们肯定就异常了

这里写图片描述

此篇纯属娱乐,哈哈,有兴趣的加群:555974449

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘某人程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值