Error codes lookup - II
23 May 2008In 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.
Related Posts:
- Error codes lookup - I
- JNDI lookup with a corbaname URL
- JNDI - Fixed qualified names
- Handling ASCII character set in Java (I)
- Java Naming and Directory Interface - II
- Using the hibernate objects
- Handling ASCII character set in Java (II)
- Validating ASCII character set
- Asynchronous method calls - I
- Introduction to Hashtable
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
Reflection? Really? Wow.