|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Throwable
java.lang.Exception
com.ccg.util.FormatException
public abstract class FormatException
A base class to simplify the creation of localized exceptions.
This class is intended to make it easier for developers to design and implement localized exception messages on a package basis.
To get a good idea of how this works, refer to the simple working example FException.java, its messages in their default language in the file msgs/FormatException.properties, and a "pig latin" version of the strings in msgs/FormatException_en_PL.properties.
Here are the basics in using this class:
com.ccg.messages.MessageException
class for a working example.
E1000=Failed to connect to host {0}.
E1001=Port {0} in use at {1}.
E1002=You can not leave the user ID field blank.
NOTE: By default, this class will ALWAYS attempt to return a
localized message. By ALWAYS attempting to localize, one can simply
pass the exception ID's to the constructor (like: "E0002") thus
removing the need for long strings in the source code. If you don't
want your error strings always localized, you should override the
isAlwaysLocalized method and have it
return false instead of true in your derived class.
| Constructor Summary | |
|---|---|
FormatException()
Construct the exception without a message/tag. |
|
FormatException(String s,
Object[] args,
Throwable t)
Associate a tag/message and optional format arguments with the exception. |
|
| Method Summary | |
|---|---|
String |
getLocalizedMessage()
Get a localized representation of the exception. |
String |
getMessage()
Get string representation of the exception. |
protected abstract ResourceBundle |
getResourceBundle()
This method is used when a localized message is being used. |
Throwable |
getUnderlying()
Get the underlying Throwable which triggered this exception |
boolean |
hasUnderlying()
Is there an underlying problem associated
with this exception? |
boolean |
isAlwaysLocalized()
Should all messages be localized? |
protected ResourceBundle |
loadResourceBundle(Package p)
Helper method to load stock error messages for a package. |
void |
setUnderlying(Throwable val)
Set the underlying Throwable which triggered this exception |
| Methods inherited from class java.lang.Throwable |
|---|
fillInStackTrace, getCause, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public FormatException()
Typically one doesn't use this method - as it pretty much defeats the translation capabilities of this class.
FormatException(String,Object[],Throwable)public FormatException(String s, Object[] args, Throwable t)
This is the typical way in which one constructs these objects. It allows one to specify a default message (like: "E1000:config file {0} not found"), and optional set of arguments which can be subsituted into the message.
s - The ID of the message in the resource file to use with the
exception (can be null - but it isn't recommended)args - The set of arguments to subsitute into the string as defined
by the MessageFormat class. If you don't require
variable subsitution, then pass null.t - The underlying problem which caused us to throw this exception
(or null if there wasn't one).setUnderlying(java.lang.Throwable)| Method Detail |
|---|
public boolean hasUnderlying()
underlying problem associated
with this exception?
Sometimes one will want to create custom exceptions which have an associated underlying exception. For example, if you
getUnderlying()public void setUnderlying(Throwable val)
Throwable which triggered this exception
val - New Throwable value to assign.getUnderlying()public Throwable getUnderlying()
Throwable which triggered this exception
setUnderlying(java.lang.Throwable)public String getMessage()
This method returns the default string representation of the exception. This has several different possibilities:
associated Exception, then null is returned.
associated
Exception, then message from the underlying exception is
returned.
format the string (subsituting arguments
following the standard MessageFormat protocol).
Note, if the class localizes ALL
messages, then any invocation of this method will just be handed
off to the getLocalizedMessage
method.
getMessage in class ThrowablegetLocalizedMessage()public boolean isAlwaysLocalized()
This method will return "true" if all error messages should be
localized. By default, this method returns "false" indicating the
that string the error is constructed with can be used as the
normal text for the error message. If this
method returns true, then the getLocalizedMessage method will be used (even when you call
getMessage()).
If you prefer to keep all of your error descriptions in your resource bundles (which is probably wise), you don't need to override this method (as the default behavior is to always use the associated resource bundle). Using this implementation, you only need to specify the "key name" of your error messages (it reduces the size of the strings in your actual code as you can use strings like: "E0001" instead of "E0001:{0} is not a valid name".
If however, you prefer that the default text be exactly what you entered in your code, then you should override this method (make it return false), and then make sure that you use the "KEY:TEXT" form for all of your error messages in your program.
getLocalizedMessage(),
getMessage()public String getLocalizedMessage()
This method attempts to translate the error message into its "localized" form. In order for a message to be translated, ALL of the following must be true:
resource bundle must be
associated with the object.
If we are unable to translate the message to its localized
form, then the non-localized form will be
returned.
NOTE: This method should NEVER invoke the getMessage method directly (it can use:
super.getMessage()).
getLocalizedMessage in class ThrowablegetMessage(),
getResourceBundle()protected abstract ResourceBundle getResourceBundle()
This method is used if a request for a localized exception message has been requested. It method returns the resource bundle if its possible to lookup a localized message. This method will return null if not possible - but it should NEVER throw an exception.
You MUST override this method! The easy way to do this looks
something like the following:
private static
ResourceBundle _Resources;
protected ResourceBundle getResourceBundle() {
if (_Resources == null) {
_Resources = loadResourceBundle(this.class.getPackage());
}
return _Resources;
}
To use the above, you then create a "msgs/FormatExcection.properties" file (and its language derivatives) for your package.
Resource bundle to lookup localized
message from, or null if no localized information is
available.getLocalizedMessage()protected ResourceBundle loadResourceBundle(Package p)
This helper method is used to load stock error messages for a package. You pass it the package which you want to get the error message resources for, it will then try and return the appropriate resource bundle.
This method is intended as a helper method for derived classes
which implement the getResourceBundle() method.
Take a look at the "com.ccg.messages.MessageException" class, its source files and the "com/ccg/messages/msgs/FormatException.properties" file for a example of how this works.
p - The package to get the error messages for - if you pass null,
we will assume no package and look for
"msgs/FormatException.properties".
ResourceBundle with the error
message translations for the package. If not found, we will
return a custom resource bundle that just returns the "key"
value as the associated data for each lookup.getResourceBundle()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||