用Java实现的一个密码验证 以及Java异常处理流程和runtime异常以及checked异常之间的区别

内容还是很简单的 主要还是为了复习一下一些基础的内容

import java.util.Scanner;

public class Index{
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		String str;
		while(true){
			str=scan.nextLine();
			try{
				for(int i=0;i<str.length();++i)
					if(!Character.isLetter(str.charAt(i))&&!Character.isDigit(str.charAt(i)))
						throw new InvalidCharException("格式错误!");
				if(str.length()<8){
				    throw new InSuffcientLenException("长度不足!");		
			    }		
		   }catch(InvalidCharException e){
			   System.out.println(e.getMessage());
			   continue;
		   }catch(InSuffcientLenException e){
			   System.out.println(e.getMessage());
			   continue;
		   }
			scan.close();
			break;
		}
		
		StringBuffer encryptedStr =new StringBuffer(str.length());
		for(int i=0;i<str.length();++i){
			if(Character.isUpperCase(str.charAt(i))){
				encryptedStr.append((char)('Z'-str.charAt(i)+'A'));
			}else if(Character.isLowerCase(str.charAt(i))){
				encryptedStr.append((char)('z'-str.charAt(i)+'a'));
			}else{
				encryptedStr.append((char)('9'-str.charAt(i)+'0'));
			}
		}
		
		System.out.println(encryptedStr.toString());
	}
}

 //自定义的异常类
class InvalidCharException extends RuntimeException{
	InvalidCharException(){}
	
	InvalidCharException(String message){
		super(message);
	}
}

class InSuffcientLenException extends RuntimeException{
	InSuffcientLenException(){}
	InSuffcientLenException(String message){
		super(message);//这里的message就是getMessag的返回值
	}
}
这里重点还是要复习一下Java异常的处理流程

1.JVM发现异常之后实例化该异常

2.对异没有处理 程序结束 异常会被抛给JVM自动处理

  对异常有处理 try块捕获异常 catch块匹配异常  执行finally(不管catch块是否匹配异常 程序都会执行finally)

 如果异常匹配 那么程序就从匹配的catch开始继续向下执行

 如果异常没有匹配,那么程序就会结束 异常会抛个JVM进行自动处理

runtimeException和checkedException之间的区别

最主要的区别就是runtimeException是在程序运行的时候发生的 并且不强制要求用户处理异常

并且还是要注意一下Java基本类型转换之间的问题 以及String和StringBuffer之间的区别

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值