Java 定制的例外总是给我;异常:未知异常“;

Java 定制的例外总是给我;异常:未知异常“;,java,exception,exception-handling,Java,Exception,Exception Handling,在我的程序中,我创建了一个定制的异常类实用程序,当用户输入一个非整数时,它应该返回一个异常“Exception:Non-Integer value entered”。然而,每次我运行程序时,我都会得到“异常:未知异常”。请问,有人能告诉我正确的路线吗?非常感谢你 import java.util.Scanner; import java.util.InputMismatchException; class Date { public static final int JAN = 1;

在我的程序中,我创建了一个定制的异常类实用程序,当用户输入一个非整数时,它应该返回一个异常“Exception:Non-Integer value entered”。然而,每次我运行程序时,我都会得到“异常:未知异常”。请问,有人能告诉我正确的路线吗?非常感谢你

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) );
    }
}
import java.util.Scanner;
导入java.util.InputMismatchException;
上课日期
{
公共静态最终int JAN=1;
公共静态最终int FEB=2;
公共静态最终int MAR=3;
公共静态最终int APR=4;
公共静态最终整数可能=5;
公共静态最终int JUN=6;
公共静态最终int-JUL=7;
公共静态最终int 8月=8;
公共静态最终int SEP=9;
公共静态最终int OCT=10;
公共静态最终int NOV=11;
公共静态最终int DEC=12;
静态布尔值年份(整年)
{   
回报率(((年度%100!=0)和((年度%4==0)| |((年度%400)==0));
}
int returnDaysInMonth(int年,int月)
{
int[]daysInMonth={0,31,28,31,30,31,31,31,30,31};
int day=0;//问题:从未使用过此选项
日=日/月[月];
如果(年)
{
如果(月=二月)
{
day++;
}
}
回归日;
}
int returnDaysInYear(int年)
{
报税表(年份)?366:365;
}
整数确定整数日期(整数年、整数月、整数日)
{
int累加器=0;
对于(int methodYear=1900;methodYearreturnDaysInYear(年份);year++)
{
julianDate-=每年的返回日(年);
}
回归年;
}
int determineMonth(int julianDate)
{
整月=0;
整年=0;
year=determinateyear(year);//问题:这从未被使用过
对于(年份=1900;julianDate>returnDaysInYear(年份);year++)
{
julianDate-=每年的返回日(年);
}
对于(月=0;julianDate>returnDaysInMonth(年,月);月++)
{
julianDate-=返回日每月(年,月);
}
返回月份;
}
int determineDay(int julianDate)
{
整月=0;
整年=0;
对于(年份=1900;julianDate>returnDaysInYear(年份);year++)
{
julianDate-=每年的返回日(年);
}
对于(月=0;julianDate>returnDaysInMonth(年,月);月++)
{
julianDate-=返回日每月(年,月);
}
返回julianDate;
}   
int queryForValidYear()
{
整年=0;
试一试{
做{
年份=Utility.queryForInt(“输入年份”);
如果(!isYearValid(年))
System.out.println(“错误:年份必须高于1900。”);
}而(!isYearValid(year));
}捕获(输入不匹配异常输入)
{
抛出新的DateException(“异常:输入了非整数值”);
}捕获(例外e)
{
抛出新的DateException(“异常:未知异常”);
}
回归年;
}
int queryForValidMonth()
{
整月=0;
月份=0;
试一试{
做{
month=Utility.queryForInt(“输入月份”);
如果(!isMonthValid(月))
System.out.println(“错误:月份必须是1-12。”);
}而(!isMonthValid(月));
}捕获(输入不匹配异常输入)
{
抛出新的DateException(“异常:输入了非整数值”);
}捕获(例外e)
{
抛出新的DateException(“异常:未知异常”);
}
返回月份;
}
int queryForValidDay(int年,int月)
{
整日=0;
日=0;
试一试{
做{
day=Utility.queryForInt(“输入一天”);
如果(isDayValid(年、月、日))
System.out.println(“错误:当月天数错误”);
}而(!isDayValid(年、月、日));
}捕获(输入不匹配异常输入)
{
抛出新的DateException(“异常:输入了非整数值”);
}捕获(例外e)
{
抛出新的DateException(“异常:未知异常”);
}
回归日;
}
布尔值isYearValid(整数年)
{
回报率((年>=1900));
}
布尔值isMonthValid(整数月)
{
回报((月>=1&&month=1)&&day很明显:
排队

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

捕获的异常不是类型为
inputmaschException
,而是其他类型的异常,您正在使用
exception
捕获另一个异常,并作为
DateException重新运行(“异常:未知异常”)
这就是消息的来源。可能是
NoTouchElementException
-如果输入已耗尽,或者
IllegalStateException
-如果此扫描仪已关闭(这些都是nextInt()抛出的).

您正在用正确的消息捕获自己的
DateException
,然后用错误的消息重新播放新消息

  • queryForInt
    中,
    nextInt
    抛出一个
    InputMismatchException
    ,您捕获该异常,然后抛出一个带有所需消息的
    DateException
  • 然后,在例如
    queryForValidDay
    中,您捕捉到
    DateException
    a