就是自定义异常,创建了这个异常类以后就可以直接调用这个异常了,
拜托了

解决方案 »

  1.   

    public class MyException extends Exception{
    public MyException()  {}
        public MyException(String message) {
            super(message);
        }
    }
    不明白LZ什么意思,自定义异常不就是这样吗
      

  2.   

    最简单的:
    import java.lang.reflect.*;class DoubleException extends Exception{
    public String toString(){
    return "It's not a double value!";
    }
    }public class IsDouble{
    double d = 1.0;
    int j = 1;

    public static void main(String args[]){
    try{
    Class c = Class.forName("IsDouble");
    Field fieldlist[] = c.getDeclaredFields();           for (int i = 0; i < fieldlist.length; i++) {  
             Field fld = fieldlist[i];  
            
                if(fld.getType().getName().equals("double")) 
                 throw new DoubleException();            
                else
                 System.out.println(fld.getName() +": "+ fld.getType());
            }
    }catch (Exception e) {  
               e.printStackTrace();  
        } 
    }
    }
    输出:
    It's not a double value!
    at IsDouble.main(IsDouble.java:22)
      

  3.   

    自定义异常 楼上已有何时抛:
    double d = 1.0;
    Double d = new Double(d);
    if(d instanceof Double)   throw new DoubleException(); 
      

  4.   

    public class IsDouble {
    public static boolean isDou(Number n){
    boolean b=false;
    try {
    b=Class.forName("java.lang.Double").isInstance(n);
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    if(!b){
    try {
    throw new MyException("is not a Double");
    } catch (MyException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return b;
    }
    public static void main(String[] args){
    Double d=1.0;
    int i=1;
    System.out.println(isDou(d));
    System.out.println(isDou(i));
    }
    }
    测试结果:true
    day05.MyException: is not a Double
    at day05.IsDouble.isDou(IsDouble.java:14)
    at day05.IsDouble.main(IsDouble.java:26)
    false
    就用我1楼的那个异常
      

  5.   

    自定义异常!不 就是定义一个自己的异常!用来extends 非受检异常!最终的目的是不受检异常转化成
    非受检异常!
    public class MyException extends RuntimeException{
    private static final long serialVersionUID = -1663367068727673091L; public DAOException() {
    super();
    // TODO Auto-generated constructor stub
    } public DAOException(String message, Throwable cause) {
    super(message, cause);
    // TODO Auto-generated constructor stub
    } public DAOException(String message) {
    super(message);
    // TODO Auto-generated constructor stub
    } public DAOException(Throwable cause) {
    super(cause);
    // TODO Auto-generated constructor stub
    }
      }
      

  6.   

    我不是很明白,如果你把方法参数设定为 public void test(double num); 这样的话,还有必须去判断 num 是 double 类型么?
      

  7.   

    2#把异常放错地方了吧???import java.lang.reflect.*;class DoubleException extends Exception
    {
       
    private static final long serialVersionUID = 1L; public String toString(){
            return "It's not a double value!";
        }
    }public class IsDouble{
        double d = 1.0;
        int j = 1;
        
        public static void main(String args[]){    
            try{
                Class<?> c = Class.forName("IsDouble");            
                Field fieldlist[] = c.getDeclaredFields();              for (int i = 0; i < fieldlist.length; i++) {  
                    Field fld = fieldlist[i];  
                    
                    if(fld.getType().getName().equals("double")) 
                             System.out.println(fld.getName() +": "+ fld.getType());    
                    else
                        throw new DoubleException();   
                }
            }catch (Exception e) {  
                   e.printStackTrace();  
            } 
        }
    }这样才能判断类中所有的变量是不是double类型