使用java 语言写一段代码定义一个异常类当输入字符是字母时将产生异常
public class LetterInputException extends Exception { public LetterInputException() { super("Input contains letters!"); } }
// 在使用的地方 try { // some code if (input.matches(".[a-zA-Z]+.")) { throw new LetterInputException(); } // some code } catch (LetterInputException e) { System.out.println(e.getMessage()); }
原文地址: https://www.cveoy.top/t/topic/cfSt 著作权归作者所有。请勿转载和采集!