gpt4 book ai didi

java - 自定义异常一直给我 "Exception: Unknown exception"

转载 作者:行者123 更新时间:2023-11-30 08:37:15 25 4
gpt4 key购买 nike

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

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) );
}
}

最佳答案

嗯,这很明显:在行中

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

捕获的异常不是 InputMismatchException 类型,而是其他类型,您正在使用 Exception 捕获另一个异常并重新生成为 DateException ("Exception: Unknown exception"); 这就是消息的来源。可以是 NoSuchElementException - 如果输入耗尽或 IllegalStateException - 如果此扫描器已关闭(这些都是 nextInt() 抛出的内容)。

关于java - 自定义异常一直给我 "Exception: Unknown exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37367819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com