Java复习之知识点整理(五)----异常 + package + jar包

37 篇文章 3 订阅
异常
-----------------------------------------------------------------------------------
1.RuntimeException 以及其子类异常,在函数中被throw抛出,可以不try catch 不在函数上声明throws
2.继承需要注意:
①当一个方法被子类覆盖时,子类同名方法如果抛出异常,那么抛出的异常必须是父类的异常或者父类异常的子类。
②如果父类抛出多个异常,那么子类也要抛出多个异常,或者多个异常共有的子类异常。不能抛出新的父类没有的异常
③父类不抛出异常,那么子类也不能抛出异常

自定义异常
-------------------------------------------------------------------------------------
class AgeException extends Exception {
	public String info;

	public AgeException(String info) {
		this.info = info;
	}

	public AgeException() {
		this("年龄异常!");
	}

	public void printException() {
		System.out.println(info);
	}
}

class AgeTooBigException extends AgeException {
	public AgeTooBigException() {
		super("年龄太大!!!");
	}
}

class AgeTooSmallException extends AgeException {
	public AgeTooSmallException() {
		super("年龄太小!!!");
	}
}

class Person {
	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) throws AgeException {
		if (age > 200) {
			throw new AgeTooBigException();
		} else if (age < 0) {
			throw new AgeTooSmallException();
		}
		this.age = age;
	}
}

Package
-----------------------------------------------------------------------------------
1.对类文件进行分类管理
2.包必须是源文件的第一句话
3.其实类名的全称为: 包名.类名
4.包也是一种封装形式


包与包之间的访问以及修饰符的关系
----------------------------------------------------------------------------------------
public protect default private
同类 √ √ √ √

同包 √ √ √

子类 √ √

不同包 √

cmd命令行打jar包
-------------------------------------------
1.cmd + jar cvf xxx.jar xxx.class xx.class xxxx.class
将 xxx.class ,xx.class, xxxx.class三个class文件,打入xxx.jar包中
注意 xxx.class ,xx.class, xxxx.class , xxx.jar 都是相对路径;必要时请使用绝对路径

2.cmd + jar cvf xxx.jar -C d:\xxx\xx/ *.class
将 d:\xxx\xx 文件夹下的所有 *.class文件打包到当前目录下,并且包名为xxx.jar

3.cmd + jar -tf xxx.jar
查看当前目录下xxx.jar包里面的文件

4.cmd + jar -cvfe xxxx.jar xx.xxx.xx.Dog -c ./ .
将当前目录下的所有文件打成xxxx.jar包,并且入口mian 函数为 xx.xxx.xx包下的Dog

5.cmd + java -jar xxxx.jar
运行当前目录下的xxxx.jar包,前提是xxxx.jar包有入口函数

6.cmd + java -cp d:\xx\xxx\x\xxx.jar xx.xxx.xx.Dog
运行d:\xx\xxx\x\xxx.jar包,并且以xx.xxx.xx.Dog为入口类


练习一:
三角形类自定义异常
定义三角形类(Triangle{a,c,b}),
要求:
a.两边和 > 第三边 ,抛出异常
b.a|b|c > 0 ,抛出异常
-------------------------------------------------------------------------------------
public class Ts01 {

	public static void main(String[] args) throws Exception {
		Triangle t = new Triangle(1, 5, 6);
		Triangle t1 = new Triangle(-1, 5, 6);
		Triangle t2 = new Triangle(1, 5, 5);
	}
}

class Triangle {
	private int a;
	private int b;
	private int c;

	public Triangle(int a, int b, int c) throws Exception {
		if (a <= 0 || b <= 0 || c <= 0)
			throw new TooSmallException();
		if (!checkLong(a, b, c))
			throw new IllegalException();
		this.a = a;
		this.b = b;
		this.c = c;
		System.out.println("三角形的边长为:" + a + "," + b + "," + c);
	}

	public boolean checkLong(int a, int b, int c) {
		if (a + b <= c || a + c <= b || b + c <= a)
			return false;
		else
			return true;
	}
}

class TooSmallException extends Exception {
	public String info;

	public TooSmallException() {
		this("发生异常:边长必须大于0!!!");
	}

	public TooSmallException(String info) {
		this.info = info;
	}
}

class IllegalException extends Exception {
	public String info;

	public IllegalException() {
		this("发生异常:两边之和必须大于第三边!!!");
	}

	public IllegalException(String info) {
		this.info = info;
	}
}



全文完!




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值