2

In my program, I have created a customized Exception class Utility and it should return an exception of "Exception: Non-Integer value entered" when the user inputs a NON-INTEGER. However, everytime I run the program, I keep getting "Exception: Unknown exception". Please, can anyone direct me to the correct path? Thank you so very much.

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) );
    }
}
3
  • 1
    Can you post a Minimal, Complete, Verifiable Example? There is a lot of extraneous code here. Commented May 21, 2016 at 20:49
  • 1
    You should post a shallow version of your problem. Please. This is unreadable. Commented May 21, 2016 at 20:50
  • 4
    @gordon sung You're hiding exceptions, which is never good. In a number of places you do this kind of thing: try { someCode() } catch (ExpectedException x) { ...handle x .... } catch(Exception whatever) { throw new UselessException() } and then you complain that you're getting UselessException thrown. So stop throwing it. Commented May 21, 2016 at 20:56

2 Answers 2

1

Well it's kind of obvious: in the lines

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

the exception that is caught is not of type InputMismatchException but rather some other, and you are catching this other one with Exception and re-trowing as DateException("Exception: Unknown exception"); and that is where the message comes from. Could be either NoSuchElementException - if input is exhausted or IllegalStateException - if this scanner is closed (these are all what nextInt() throws).

Sign up to request clarification or add additional context in comments.

Comments

1

You're catching your own DateException with the right message, then rethrowing a new one with the wrong message.

  • In queryForInt, nextInt throws an InputMismatchException, which you catch, then throw a DateException with the message that you want.
  • Then, in e.g. queryForValidDay, you catch that DateException as a plain Exception and throw a new DateException with the message that you don't want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.