java自定义异常的使用及注意事项

定义一个异常类:

/**
 * 自定义异常通常都是通过继承一个异常来实现的
 * 1.Throwable
 * 2.Exception
 * 3.RuntimeException
 * 4.自定义异常实现是,重写父类的构造方法
 * 5.异常对象本身是没有任何意义,只是一个有意义的标识
 * 6.受检异常:Exception:
 * 定义方法时必须声明所有可能会抛出的exception:在调用这个方法是,必须捕获它的checked exception,不然
 * 就得把它的exception传递下去:exception是从java.lang.Exception类衍生出来的。例如:IOException
 * SQLException就属于Exception
 * 7.受检异常:RuntimeException
 * 再定义方法是不需要生命会抛出runtime exception:在调用这个方法是不需要捕获这个runtime exception:
 * RuntimeException是从java.lang.RuntimeException或java.lang.Error类衍生出来的
 * 例如:NullPointException,IndexOutBoundsException就属于RuntimeException
 * 
 * assert关键字,表示断言:
 * 当程序执行到某个固定的位置时候,程序中的某个变量的取值肯定是预期结果,那么这中操作可以使用断言完成
 * 断言的操作语法:
 * assert表达式:
 *
 */
public class MyException extends Exception{
	public MyException(){}
	public MyException(String message){
		super(message);
	}
	public static void main(String[] args) {
	// TODO Auto-generated method stub
	}
}

下面我们来测试其用法
1.构造用户类

public class User {
	private String username;
	private String password;
	private int age;
	private String sex;
	public User(){}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + ", age=" + age + ", sex=" + sex + "]";
	}
	public User(String username, String password, int age, String sex) {
		super();
		this.username = username;
		this.password = password;
		this.age = age;
		this.sex = sex;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
}

构造UserService类

public class UserService {


	public User login(String username,String password) throws MyException{
		
		if(!"admin".equals(username)){
			throw new MyException("用户名错误");
		}
		if(!"12345".equals(password)){
			throw new MyException("密码错误");
		}
		User user=new User("admin","12345",18,"男");
		return user;
	}

}

构造login类

/**
 * 面试题:
 * Exception 与RuntimeException
 * Exception:受检异常,在编程期检查,调用输出这个异常的方法是,必须显示的使用try...catch...
 * RuntimeException:非受检异常,在运行期检查,调用输出这个异常的方法是,可以不用显示的使用try...catch...
 * 在使用自定义异常时,根据实际的业务要求,来决定使用那个作为父类
 */
import java.util.Scanner;

public class loginDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		System.out.println("请输入用户名:");
		String name=input.nextLine();
		System.out.println("请输入密码:");
		String pass=input.nextLine();
		
		UserService us=new UserService();
		try {
			User user=us.login(name, pass);
			System.out.println("登陆成功!");
			System.out.println(user);
			
		} catch (MyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

登陆成功,无异常
在这里插入图片描述
密码错误:
在这里插入图片描述
用户名错误:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44117272/article/details/89470396
今日推荐