Hey all I am currently a java beginner and I would like to ask how would I return control to my main if ever I encounter an exception created by me?
For example:
public void checkIn(int roomNumber, String occupantName, int day)
throws ExceptionHandler {
int checkValue = this.employingHotel.checkIn(roomNumber, occupantName, day);
switch (checkValue) {
case -5:
throw new ExceptionHandler ("Inexistent Room for Check In");
case -6:
throw new ExceptionHandler ("Occupant Already Exists");
default:
break;
}
}
This code here returns an exception whenever an error is found within the underlying classes of this method's owner. So when I run my program I see in the output that after it displays an exception it does not run the other parts of the code in my main, which is what I didn't want to happen.
I've still got a little bit of processing down my main and I would like to perform those processes along with printing of other details from them.
So how do I return control back to my main whilst recognizing this error?
And lastly, based on something I googled earlier, I created this exception handling class.
public class ExceptionHandler extends Exception {
public ExceptionHandler(String message) {
super(message);
}
}
This is supposed to handle all the errors in my package, but I feel a bit uneasy about this class because, I think exceptionhandler codes are not this simple? I mean are they just simply for outputting error messages? or am I grasping the requirement of "create an exception class for your project" concept wrong? I really think I am missing something here.
Thank you all.
EDIT:
I would also like to add that, I am not entirely sure if we should have an exception handler class of our own in our programs. I am just assuming we should since we just tackled this lesson last week, but we also took up simple try catch statements, so I am now thinking that what if I can just handle my errors with simple try catch clauses and put in my catch statements a simple System.out.println(<Error message here>) so that my execution of main wouldn't be interrupted?