I'm still restructuring the code I have been given to update on my current work project, and I've come to the point where I'm looking at how the code handles an 'exceptional' input, that doesn't conform to the business logic.
In essence, the code stitches together various different excel spreadsheets, tarts them up and lets the user 'download' them to their workspace for later analysis.
This is what I have set in the code:
If the code ends up (anywhere) trying to, for instance, import an empty spreadsheet into the final work book (something that should never happen, under the business logic) then write out to a LinkedList variable (ErrorList) in a static class (MyVariables) like so, the error:
MyVariables.ErrorList.add("My Specific Error Text");
Then I throw an exception like so:
throw new PICNICException();
Then in the frame that called the trail of methods, the exception is finally caught and I can write, something like this:
catch(PICNICException Pe){
JOption.showMessageDialog(This, MyVariables.ErrorList.getLast(), "Title Relevant to what Code is Doing", JOption.ERROR_DIALOG)
Pe.printStackTrace();
}
I have feel I have to do this as, I have no way to pop up the Message Dialog (which is the desired behaviour on erroring) from within the method that's thrown this exception. Without completely refactoring the entire code base, and making a singleton that can be accessed anywhere to handle these message dialogues, I don't see any other way of doing this.
I can't help but feel this is a gigantic hack, and there is a better method for doing this.
Am I handling my exceptions in the best possible manner, given my situation? What would the best practice be?
PICNICException
is to show a popup asking the user to leave the computer and let someone else take over.PICNICException
is a class like any other, is it not?.PICNICException e = new PICNICException(); e.setStuff("hi"); throw e;
and then on the catching end:catch(PICNICException e){ showMessage(e.getStuff());}
. You can add any kind of first-class (not a string that you parse) custom data to the exception, throw it and retrieve it on the catching end.