Re: how to handle errors and preconditions




"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




.



Relevant Pages

  • RE: freebsd-hackers Digest, Vol 200, Issue 7
    ... Updated Driver for 3945ABG Intel 3945ABG Wireless LAN ... sysctlinterface ... Jan 27 10:30:39 vaio kernel: bus_dmamem_alloc failed to align memory properly. ... debug: all: oid: 0 2 440 ...
    (freebsd-hackers)
  • Re: multithreaded debugging
    ... I think you hit he problem: the kernel should run in order to control the ... machine but the interface could be stopped without any problem. ... I need to use debug mechanisms because EVC4 doesn't allow me to do. ... >>> the real time threads are resumed but, cause their inactivities, they ...
    (microsoft.public.windowsce.platbuilder)
  • Re: b43 problem.
    ... wlan0: authenticated ... b43-phy0 debug: Wireless interface stopped ...
    (Fedora)
  • b43 problem.
    ... with a Broadcomm 4318 wireless driver. ... wlan0: authenticated ... b43-phy0 debug: Wireless interface stopped ...
    (Fedora)
  • Re: Simpler syntax for properties?
    ... No - you've just broken your public interface, ... > The OP didn't indicate the need to validate values to trace in debug. ...
    (microsoft.public.dotnet.languages.csharp)