0

I am working through a java textbook, and for one of the programs I have to write I have to handle my custom exceptions while converting standard time to military time. I used inner classes to handle the three classes in one file. I am not 100% sure this works, I haven't found anything that says it wont, or anything that says it will so far. I am probably making this way harder than it needs to be, but the book says I need the three classes with their specific names.

I have tried following what the IDE tells me, it says No enclosing instance of type InvalidTimeException is accessible. Must qualify the allocation with an enclosing instance of type InvalidTimeException (e.g. x.new A() where x is an instance of InvalidTimeException). To me this tells me to put throws new InvalidTimeException.new InvalidHrException(); this doesn't work. SO I tried setting it up like this throws new InvalidTimeException(new InvalidHrException)

Exception Class

@SuppressWarnings("serial")
public class InvalidTimeException extends Exception {

    public InvalidTimeException () {
        System.out.println("Unknown Time error: An error has occoured in the program... shutting down.");
    }

    public InvalidTimeException (String var) {
        System.out.println("Input Mismatch Exception: \"" + var + "\" is not a number.");
    }

    public class InvalidHrException extends Exception {
        public InvalidHrException () {
            System.out.println("Error in hour input: An error has occoured with the hour input");
        }
    }

    public class InvalidMinException extends Exception {
        public InvalidMinException () {
            System.out.println("Error in minute input: An error has occoured with the minute input");
        }
    }

    public class InvalidSecException extends Exception {
        public InvalidSecException () {
            System.out.println("Error in seconds input: An error has occoured with the seconds input");
        }
    }
}

Example Method

public static void hourChecker (int var) throws InvalidHrException, InvalidTimeException {
        if (var <= 0) {
            throw new InvalidTimeException(new InvalidHrException());
        }
    }

I would expect this to let me throw the exception of the inner class InvalidHrException, from the InvalidTimeException class. However no matter what I try there seems to be an error. I am not sure if I just over complicated things or what. Thank you for your help.

EDIT

current code to make error classes work

public InvalidTimeException (InvalidHrException IHE) throws InvalidHrException {
        // just lets the program hit the inner classes
    }

current example method

public static void hourChecker (int var) throws InvalidHrException, InvalidTimeException {
        if (var <= 0 || var > 12) {
            throw new InvalidTimeException.InvalidHrException();
        } else {
            return;
        }
    }
Hunter
  • 9
  • 4
  • You probably need a static inner class. – glee8e May 06 '19 at 15:01
  • Related question: [Java inner class and static nested class](https://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) – glee8e May 06 '19 at 15:02
  • Omg thank you that did it, wow I feel kinda dumb now. I will probably just delete this because it was that simple. Thnaks though I am pretty sure you'll still see this. – Hunter May 06 '19 at 15:03
  • @glee8e So upon further inspection that doesn't really work the way I want it. It made me create a constructor in the error class that had all of the inner classes as parameters. So It works but in a roundabout way that doesn't feel right. Is there another way to do this? I am honestly only pursuing this because I am learning now, I should learn the right way. I'll add an edit right now to show you what I currently have to make it work. – Hunter May 08 '19 at 13:16
  • It looks like what you really need is use `InvalidTimeException` as an abstract base class and let the `InvalidHrException` and its friends to be the subclass of that base class. `InvalidHrException` can still be a static inner class within that base class if you desire. – glee8e May 08 '19 at 13:40

0 Answers0