throw自定义异常

package com.exception;

class vv{
	String name;

	public String getName() {
		return name;
	}

	public void setName(String name) throws Exception{
		if(name.equals("男")||name.equals("女")){
				this.name = name;
		}
		else{
			throw new Exception("性别必须是男或女");
		}
	}

	public vv(String name) {
		super();
		this.name = name;
	}

	public vv() {
		super();
	}
	
	
}

public class throww   {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
       vv v=new vv();
       try {
		v.setName("那你");
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
      System.out.println(v.getName()); 
	}

}

猜你喜欢

转载自blog.csdn.net/liujucai/article/details/81181282
今日推荐