自定义异常类和抛出异常的方法

package com.sxt.exception;

public class Programmer implements Comparable<Programmer>{

	private String name;
	private int age;
	private double weight; //体重单位:kg
	private double hight;  //身高单位:cm
	
	public Programmer() {
		super();
	}

	public Programmer(String name, int age, double weight, double hight) throws NameException, 
	                                    AgeException, WeightException, HightException {
		super();
		if(name.trim().length() == 0 || name == null || name.trim().length() > 20) {	
			throw new NameException("人名不能为空,并且不能超过二十个字符!");
		}
		this.name = name;
		
		if(age < 0 || age > 120) {	
			throw new AgeException("年龄不能小于0岁或者大于120岁,请核实!");
		}
		this.age = age;
			
		if(weight < 40 || weight > 100) {	
			throw new WeightException("此程序员身体太差,或胖或瘦,请核实!");
		}
		this.weight = weight;
		
		if(hight < 150 || hight > 200) {
			throw new HightException("此人身高可能过低或者过高,请核实!");
		}
		this.hight = hight;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) throws NameException {
		if(name.trim().length() == 0 || name == null || name.trim().length() > 20) {
			
			throw new NameException("人名不能为空,并且不能超过二十个字符!");
		}
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) throws AgeException {
		if(age < 0 || age > 120) {
			
			throw new AgeException("年龄不能小于0岁或者大于120岁,请核实!");
		}
		this.age = age;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) throws WeightException {
		if(weight < 40 || weight > 100) {
			
			throw new WeightException("此程序员身体太差,或胖或瘦,请核实!");
		}
		this.weight = weight;
	}

	public double getHight() {
		return hight;
	}

	public void setHight(double hight) throws HightException {
		if(hight < 150 || hight > 200) {
			
			throw new HightException("此人身高可能过低或者过高,请核实!");
		}
		this.hight = hight;
	}

	@Override
	public boolean equals(Object obj) {

		if(this == obj) return true;
		if(obj == null) return false;
		if(this.getClass() != obj.getClass()) return false;
		if(obj instanceof Programmer) {
		    Programmer  programmer = (Programmer) obj;
		    return this.name.equals(programmer.name) && this.age==programmer.age && 
			       this.weight==programmer.weight && this.hight==programmer.hight;
		}
		return false;
	}

	@Override
	public String toString() {
	    return this.getClass().getName() + "	name=" + name + ", age=" + age + ", weight="
				+ weight + ", hight=" + hight;
	}

	@Override
	public int compareTo(Programmer other) {
	    if(this.age > other.age) return 1;
	    if(this.age < other.age) return -1;
	    return 0;
	}
		
}

自定义异常类:NameException 
public class NameException extends Exception {

    private static final long serialVersionUID = -2992284109668400826L;

    public NameException(String message) {
        super(message);
    }

	
}

自定义异常类:AgeException
public class AgeException extends Exception {

    private static final long serialVersionUID = -707396355691365455L;

    public AgeException(String message) {
	super(message);
    }

	
}

自定义异常类:WeightException 
public class WeightException extends Exception {

    private static final long serialVersionUID = 533854535537735838L;

    public WeightException(String message) {
	  super(message);
    }
	
}

自定义异常类:HightException 
public class HightException extends Exception {

    private static final long serialVersionUID = -5100566806826020540L;

    public HightException(String message) {
	super(message);
    }
	
}

测试类:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestProgrammer {

	public static void main(String[] args) {

		List<Programmer> list = new ArrayList<>();
		Programmer programmer1 = null;
		Programmer programmer2 = null;
		Programmer programmer3 = null;
		
		try {
			programmer1 = new Programmer("李云龙", 45, 67, 180);
			programmer2 = new Programmer("佟大为", 32, 75, 185);
			programmer3 = new Programmer("刘德华", 54, 76, 175);
		} catch (NameException | AgeException | WeightException
				| HightException e) {
			e.printStackTrace();
		}
		list.add(programmer1);
		list.add(programmer2);
		list.add(programmer3);
		
		Programmer[] programmers = new Programmer[list.size()];
		list.toArray(programmers);
		Arrays.sort(programmers);
		System.out.println("按照年龄排序:");
		for(Programmer programmer : programmers) {
			System.out.println(programmer.toString());
		}

		System.out.println("==================================================================");
		System.out.println(programmer1.toString());
		System.out.println(programmer2.toString());
		System.out.println("programmer1 = programmer2?:" + programmer1.equals(programmer2));
	}

}


转载于:https://my.oschina.net/hasonger/blog/209107

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值