编写一个程序ByteSize,接收使用命令行参数输入的数字,并计算它的字节数,此程序应在数字超出字节范围时引发用户自定义异常.
存在问题:不知道该题目要如何理解...是针对byte类型的,还是针对所有数字数据类型(byte,short,int,long...),高人指教下...
class ByteSizeException extends Exception{
ByteSizeException(){
System.out.println("数字超出了字节范围");
}
}
public class ByteSize{
int byteSizeCalculate(int num){
int count=1;
while(num!=0){
num=(int)(num/2);
count++;
}
return count;
}
}

解决方案 »

  1.   

    是针对字节范围
    class ByteSizeException extends Exception{
    ByteSizeException(){
    System.out.println("数字超出了字节范围");
    }
    }
    public class ByteSize{
             public static void main(String[] args){
                      try{
                           byte b = Byte.parseByte(args[0]);
                      }catch(Exception ex){
                           throw new ByteSizeException();
                      }
             }
    }