类。再定义一个 People 类,该类中的 void printLetter(char c) throws 方法抛出
NoLetterException 异常,void printDigit(char c)方法抛出 NoDigitException 异常。
主类 ExceptionExample 对 2 个方法分别测试。
package D;
public class NoletterException extends Exception {
public NoletterException(String msg) {
super(msg);
}
@Override
public String toString () {
String str="没有字符";
return str;
}
}
public class NoDigitException extends Exception{
public NoDigitException (String msg) {
super(msg);
}
@Override
public String toString () {
String str="不包括数字";
return str;
}
}
package D;
import java.util.regex.*;
public class People {
public String str;
void PrintLetter(String s ) throws NoletterException {
String patternStr="[a-z[A-Z]]";
boolean result=Pattern.matches(patternStr, s);
if(result)
throw new NoletterException (str);
}
void PrintLetter1(String s ) throws NoDigitException {
String patternStr="[0-9]";
boolean result=Pattern.matches(patternStr, s);
if(result)
throw new NoDigitException (str);
}
/*patternStr str="";
* Pattern.matches(pattersStr,s);
*
*
*
*
* */
public static void main(String[] args) {
People people = new People();
try {
people.PrintLetter("1");
} catch (NoletterException e) {
e.printStackTrace();
}
try {
people.PrintLetter1("q");
} catch (NoDigitException e) {
e.printStackTrace();
}
}
}