私のプログラムでは、カスタマイズされた例外クラス・ユーティリティを作成しました。ユーザーが非INTEGER 。ただし、プログラムを実行するたびに、「例外:不明な例外」が表示され続けます。どうか、私に正しい経路を教えてもらえますか?どうもありがとうございました。カスタマイズされた例外は私に「例外:未知の例外」を与え続ける
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));
}
}
[最小、完全、検証可能な例](http://www.stackoverflow.com/help/mcve)を投稿できますか?ここには無関係のコードがたくさんあります。 –
問題の浅いバージョンを投稿する必要があります。お願いします。これは判読不能です。 –
@ gordon sungあなたは例外を隠していますが、これは決して良いことではありません。いくつかの場所で、このようなことをします: 'try {someCode()} catch(ExpectedException x){... handle x ....} catch(Exception whatever){throw new UselessException()}' 'UselessException'がスローされていると不平を言う。だからそれを投げつけなさい。 – Tibrogargan