Error codes lookup - II

23 May 2008

In the last post, I presented a way for lookup. Here I will introduce another approach which uses static constants to define the meaning of error codes.

I have a class which defines the error codes using String constants.

public final class ErrorCodes {
 
public static final String _00 = "No error";
public static final String _10 = "File locked.";
public static final String _20 = "File does not exist.";
public static final String _30 = "Folder does not exist.");
public static final String _40 = "File is Read Only.");
public static final String _50 = "Unknown error");
 
}
 
public static String getValue (String fieldName) throws NoSuchFieldException, IllegalAccessException
{
Field field;   
        	Class cl = SmppResponseCodes.class;
        	field = cl.getField(fieldName);
        	return field.get(cl).toString();
}
 
}

It looks easy, but the problem is how to lookup these since the codes will be thrown by the methods at run time. The getValue(…) method uses Reflection to get the error description. Let me show how to use this:

FileApplication fileApp = new FileApplication();
int code = fileApp.read(arrayList,"filename.txt");
try{
System.out.println("Code retrieved: " + ErrorCodes.getValue(code));
} catch (NoSuchFieldException e) {
log.warning(e.getLocalizedMessage());
}
catch (IllegalAccessException e) {
log.warning(e.getLocalizedMessage());
}

Hope this helps.

del.icio.us:Error codes lookup - II  digg:Error codes lookup - II  spurl:Error codes lookup - II  wists:Error codes lookup - II  simpy:Error codes lookup - II  newsvine:Error codes lookup - II  blinklist:Error codes lookup - II  furl:Error codes lookup - II  reddit:Error codes lookup - II  fark:Error codes lookup - II  blogmarks:Error codes lookup - II  Y!:Error codes lookup - II  smarking:Error codes lookup - II  magnolia:Error codes lookup - II  segnalo:Error codes lookup - II  gifttagging:Error codes lookup - II

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Error codes lookup - II

One Response to “Error codes lookup - II”

  1. Mumbly Joe Says:

    Reflection? Really? Wow.

Leave a Reply