Re: how to handle errors and preconditions
- From: "Rhino" <no.offline.contact.please@xxxxxxxxxx>
- Date: Thu, 8 Dec 2005 10:28:42 -0500
"Petterson Mikael" <mikael.petterson@xxxxxxxxxxxxxxx> wrote in message
news:dn9ho5$c8d$1@xxxxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> I have an interface that looks like this:
>
> /**
> * @author eraonel
> *
> */
> public interface IConstraint {
>
> /** Method for validation of a specific constraint */
> void validate (MomDataReader mdr);
>
>
> }
>
> All XXconstraint classes implement this interface.
>
>
> In one XXconstraint class I have the following impl. of validate ( see
> below).
>
> I only want to print the Ok! lines when I have debug on.
> Errors shall always be printed and nicely written to file.
> When a precondition is not fullfilled then I want to throw a
> ValidateException(String msg).
>
> What is a good way to approach this matter?
> All design hints are welcome.
>
> cheers,
>
> //mikael
>
> //validate
> public void validate(MomDataReader mdr){
>
> // get all actions in the mon
> MomClass [] clazzes = mdr.getAllClasses();
> for (int i = 0; i < clazzes.length; i++) { MomClass clazz = clazzes[i];
> MomAction[] actions = clazz.getAllActions();
> if (actions != null) {
> for (int j = 0; j < actions.length; j++) {
> MomAction action = actions[j];
> String actionName = action.getName();
> if(actionName.matches("[a-z][]A-Za-z0-9]*")){
> //Ok! System.out.println("Class: "+clazz.getName()+" action :
> "+actionName+"Ok!");
> }else{
>
> //Error System.out.println("Class: "+clazz.getName()+" action :
> "+actionName+"Error!");
>
> }
> }
>
> } else {//Precondition not fullfilled to validate
> System.out.println("No actions for Class: "+clazz.getName());
> }
> }
>
> }
The technique I use is to have a static final class boolean named DEBUG in
my class. I hard-code it to true or false and then have code like this
throughout the program:
if (DEBUG) System.out.println("foo: " + foo);
Rather than making DEBUG a class variable, you could also pass it in from
the invoking class or put it in an interface shared by all classes that you
want to debug. Of course, that will turn on debugging for all classes that
get executed, not just the one that is giving you trouble; since that's not
normally what I want, I use a separate DEBUG variable (constant!) for each
specific class so that I can turn each on or off separately.
Again, a DEBUG class variable is probably not the best way to handle this
problem. Assertions may be the best way to go. See
http://java.sun.com/developer/Books/javaprogramming/jdk14/javapch06.PDF for
a discussion of assertions. This approach would let you enable assertions in
a single class or in many classes from outside the program; that might be
more convenient than changing a class variable from false to true and
recompiling the program.
Rhino
.
- Follow-Ups:
- Re: how to handle errors and preconditions
- From: Oliver Wong
- Re: how to handle errors and preconditions
- References:
- how to handle errors and preconditions
- From: Petterson Mikael
- how to handle errors and preconditions
- Prev by Date: Re: need help on this problem.
- Next by Date: Re: need help on this problem.
- Previous by thread: how to handle errors and preconditions
- Next by thread: Re: how to handle errors and preconditions
- Index(es):
Relevant Pages
|
|