定制异常继续给我"例外:未知异常"

时间:2016-05-21 20:46:34

标签: java exception exception-handling

在我的程序中,我创建了一个自定义的Exception类实用程序,它应该返回" Exception:输入非整数值"的异常。当用户输入非整数时。但是,每次运行程序时,我都会得到"异常:未知异常"。请问,任何人都可以指引我走正确的道路吗?非常感谢你。

import java.util.Scanner;
import java.util.InputMismatchException;

class Date 
{
   public static final int JAN = 1;
   public static final int FEB = 2;
   public static final int MAR = 3;
   public static final int APR = 4;
   public static final int MAY = 5;
   public static final int JUN = 6;
   public static final int JUL = 7;
   public static final int AUG = 8;
   public static final int SEP = 9;
   public static final int OCT = 10;
   public static final int NOV = 11;
   public static final int DEC = 12;

   static boolean isALeapYear(int year)
   {   
      return (((year % 100 != 0) && ((year % 4 == 0 ) || ((year % 400) == 0)) ));  
   }

   int returnDaysInMonth(int year, int month)
   {
      int [] daysInMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
      int day = 0;// PROBLEM: THIS IS NEVER USED

      day = daysInMonth[month];

      if (isALeapYear(year))
      {
         if (month == FEB)
             {
            day ++;
             }
      }
      return day;        
   }

   int returnDaysInYear(int year)
   {
      return (isALeapYear(year)?366:365);
   }

   int determineJulianDate(int year, int month, int day)
   {
       int accumalator = 0; 

       for(int methodYear = 1900 ; methodYear < year ; methodYear++)
           {
         accumalator +=returnDaysInYear(methodYear);
           }
       for (int methodMonth = 1 ; methodMonth < month ; methodMonth++ )
           {
         accumalator +=returnDaysInMonth(year, methodMonth);
           }
       accumalator += day;

      return accumalator;
   }

   int determineYear (int julianDate)
   {
       int year = 1900 ; // PROBLEM: THIS IS NEVER USED
       for(year = 1900 ; julianDate > returnDaysInYear(year) ; year++)
           {
         julianDate -= returnDaysInYear(year);
           }

      return year;      
   }

   int determineMonth (int julianDate)
   {
       int month = 0;
       int year  = 0;
           year  = determineYear(year);// PROBLEM: THIS IS NEVER USED

       for(year = 1900 ; julianDate > returnDaysInYear(year) ; year++)
           {
          julianDate -= returnDaysInYear(year);
           }
       for(month = 0 ; julianDate > returnDaysInMonth(year, month) ; month++)
           {
          julianDate -= returnDaysInMonth(year, month);
           }

      return month;     
   }

   int determineDay (int julianDate)
   {
       int month = 0;
       int year  = 0;

       for(year = 1900 ; julianDate > returnDaysInYear(year) ; year++)
           {
          julianDate -= returnDaysInYear(year);
           }
       for(month = 0 ; julianDate > returnDaysInMonth(year, month) ; month++)
           {
          julianDate -= returnDaysInMonth(year, month);
           }
      return julianDate ;       
   }   

   int queryForValidYear()
   {
      int year = 0;

      try{
         do{
         year = Utility.queryForInt("Enter a year.");
            if(!isYearValid(year))
               System.out.println("Error: The year must be higher than 1900.");
         }while(!isYearValid(year));
      }catch(InputMismatchException in)
          {
                throw new DateException("Exception: Non-Integer value entered");
      }catch(Exception e)
          {
         throw new DateException("Exception: Unknown exception");
      }
      return year;     
   }

   int queryForValidMonth()
   {
      int month = 0;
          month = 0;

      try{
         do{
         month = Utility.queryForInt("Enter a month.");
            if(!isMonthValid(month))
               System.out.println("Error: The month must be 1-12.");
         }while (!isMonthValid(month)) ;
      }catch(InputMismatchException in)
      {
         throw new DateException("Exception: Non-Integer value entered");
      }catch(Exception e)
      {
         throw new DateException("Exception: Unknown exception");
      }
      return month; 
   }

   int queryForValidDay(int year, int month)
   {
      int day = 0;
          day = 0;

      try{
         do{
         day = Utility.queryForInt("Enter a day.");
            if(isDayValid(year, month, day))
               System.out.println("Error: Wrong amount of days for the month.");    
         }while (!isDayValid(year, month, day));
      }catch(InputMismatchException in)
          {
         throw new DateException("Exception: Non-Integer value entered");
      }catch(Exception e)
          {
         throw new DateException("Exception: Unknown exception");
      }
      return day;
   }

   boolean isYearValid(int year)
   {
      return ((year >= 1900));
   }

   boolean isMonthValid(int month)
   {
      return((month >= 1 && month <= 12));
   }

   boolean isDayValid(int year, int month, int day)
   {
      return ((day >= 1) && day <= returnDaysInMonth(year, month));
   }
}

 class Utility  extends Exception
 {

   static int queryForInt(String prompt)
   {
      Scanner keyboard = null;// PROBLEM: THIS IS NEVER USED
      int intValue     = 0;

      try{
         keyboard = new Scanner (System.in);
         System.out.print(prompt);
         intValue = keyboard.nextInt();
      }catch(InputMismatchException in)
          {
         throw new DateException("Exception: Non-Integer value entered");
      }catch(Exception e)
          {
         throw new DateException("Exception: Unknown exception");
      }     
      return intValue;
   }
}
class DateException extends RuntimeException 
{

    public DateException(){
        super();
    }

    public DateException(String message){
        super(message);
    }

    public DateException(String message, Throwable cause){
        super(message,cause);
    }

    public DateException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace){
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
public class DateDriver 
{

   public static void main(String[] args) throws Exception//********
   {
      DateDriver ex = new DateDriver();
      ex.displayMessage();
   }
   public void displayMessage() throws Utility
   {
      int day       = 0;// PROBLEM: THIS IS NEVER USED
      int month     = 0;// PROBLEM: THIS IS NEVER USED
      int year      = 0;// PROBLEM: THIS IS NEVER USED
      int epocDays  = 0;// PROBLEM: THIS IS NEVER USED

      Date date = null;// PROBLEM: THIS IS NEVER USED
      date = new Date();

      year      = date.queryForValidYear();
      month     = date.queryForValidMonth();
      day       = date.queryForValidDay(year, month);
      epocDays  = date.determineJulianDate(year, month, day);

      System.out.println("Year is a leap year: " + Date.isALeapYear(year));
      System.out.println("The date entered is: " + month + "/" + day + "/" + year);
      System.out.println("Days since the EPOC are " + epocDays );
      System.out.println("Determine Year Says " + date.determineYear(epocDays) );
      System.out.println("Determine Month Says " + date.determineMonth(epocDays) );
      System.out.println("Determine Day Says " + date.determineDay(epocDays) );
    }
}

2 个答案:

答案 0 :(得分:1)

嗯,这很明显: 在行

try{
      keyboard = new Scanner (System.in);
         System.out.print(prompt);
         intValue = keyboard.nextInt();
      }catch

捕获的异常不属于InputMismatchException类型,而是其他类型的异常,并且您正在使用Exception捕获另一个异常并重新编译为DateException("Exception: Unknown exception");,这就是消息来自。可以是NoSuchElementException - 如果输入已用尽或IllegalStateException - 如果此扫描程序已关闭(这些都是nextInt()抛出的那些)。

答案 1 :(得分:1)

您正在使用正确的消息捕获自己的> df[df$id %in% names(which(table(df$id)>=5)), ] id foo 3 b 0.9962453 4 b 0.8980123 5 b 0.1535324 6 b 0.2802848 7 b 0.9366375 8 c 0.8109557 9 c 0.6945285 10 c 0.1012925 11 c 0.6822955 12 c 0.3757085 13 c 0.7348635 14 c 0.3026395 15 c 0.9707223 ,然后使用错误的消息重新抛出新的消息。

  • with中,df[with(df, id %in% names(which(table(id)>=5))), ] 会抛出您抓到的subset(df, with(df, id %in% names(which(table(id)>=5)))) ,然后向DateException投掷您想要的消息。
  • 然后,在例如queryForInt,您将nextInt标记为普通InputMismatchException,并使用您不想要的邮件投放新的DateException