This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm working on an important project for school which involves parsing an input data file and processing it into a billing report. My "model" is the bill report generator which formats the report given the processed data, along with associated classes that transform the input data into processed data.
The problem is with error handling. I tried a model-view-controller implementation with a view class representing the GUI and controller classes that perform actions upon GUI events. When I open a data file, the controller directs the model to load the data file and generate the report. However, if there is an error parsing the file, it is propagated all the way up the stack to the controller's method call. I would like to notify the user of where the error occured and such with a dialog box, but the design is getting too confusing for me.
Can someone suggest how I might better implement error handling in my application? This is a very important component of the application, and I don't want it to tarnish an otherwise healthy design, or at least that's how it seems.
Unfortunately it's a desktop application. I wish error handling were built in because this is proving to be a terrible problem. My grade depends on error handling, and my design goes to waste if it's not provided adequately.
There must be a way for the data file parsing mechanism to communicate to the controller and the view that there was a problem on line x in reading the data, no?
You can make a publish-subscribe kind of thing where the model publishes errors to any and all interested subscribers. One of the subscribers could live in the controller and update the view. It honors the MVC dependency model because the subscriber depends on interfaces that belong to the model. I've done one that populates a text area; you could go further to change button states and such so that the user can't do certain things after errors.
The Java library includes Observable and Observer but I find them rather too much for this simple job. This Page describes Pub-Sub about half way down; Ctrl-F search for publish. I made a little abstract "intermediary" that I use all the time. If this path sounds interesting, I can describe it a bit more.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Sep 13, 2006
This sounds perfect. I just read this after slightly downsizing the structure of my application, and this fits in just how I like it.