使用此正则表达式.
检查字符串是否包含数字/符号等.
boolean result = false;
Pattern pattern = Pattern.compile("^[a-zA-Z]+$");
Matcher matcher = pattern.matcher("fgdfgfGHGHKJ68"); // Your String should come here
if(matcher.find())
result = true;// There is only Alphabets in your input string
else{
result = false;// your string Contains some number/special char etc..
}
引发自定义异常
尝试捕获的工作
try{
if(!matcher.find()){ // If string contains any number/symbols etc...
throw new Exception("Not a perfect String");
}
//This will not be executed if exception occurs
System.out.println("This will not be executed if exception occurs");
}catch(Exception e){
System.out.println(e.toString());
}
我只是概述了try-catch的工作原理.但是您永远不要使用一般的“异常”.始终对自己的异常使用自定义的异常.