问题:判断输入的字符串是否是16进制,如果不是就抛出异常,要求用自定义的HexFormatException异常
package homeWork5;//import java.util.ArrayIndexOutOfBoundsException;
import
import java.util.InputMismatchException;
import java.util.Scanner;
public class T0822 {
public static void main(String[] args) {
q8();
}
static void q8(){
//create a Scanner
Scanner in = new Scanner(System.in);
try {
//prompt the user to enter a string
System.out.print("Enter a hex number:");
String hex = in.nextLine();
System.out.println("输入的数是否是正确的16进制数呢?" + hexPanDuan(hex));
}
catch(HexFormatException ex) {
System.out.println("不是正确的16进制数");
}
}
public static boolean hexPanDuan(String hex) {
int count = 0;
for(int i = 0 ; i < hex.length() ; i++) {
char ch = hex.charAt(i);
if(ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
count++;
}
if(count == hex.length())
return true;
else
throw new HexFormatException();
}
}
//HexFormatException异常的自定义
class HexFormatException extends Exception{
public HexFormatException() {
super();
}
}
这个会出现错误,1.Unhandle exception type HexFormatException ,2.Unreachable catch block for HexFormatException.This exception is never thrown from the try statement body.
求帮助