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