This section describes the steps to implement exceptions with support for internationalization (I18N) based on message beans.
If you are looking for a traditional way of writing exception where the exception class is also the container for the information about the exception event, then please refer to How to write I18N exceptions .
Two base implementations of exceptions are provided by this library extension to smart-exceptions-core .
Make your exceptions subclass one of them.
/** * Signals a fault. This is the base exception for all fault in the library. */ public class MyFaultException extends AbstractMessageRuntimeException { private static final long serialVersionUID = 1L; public LibFaultException(final MyMessage messageBean) { super(messageBean); } }
The message bean is separated from the exception and provides information that provides the exception context.
@ParentMessageParam("cause=0:message") public class ServiceMessage extends AbstractMessageBean { private static final long serialVersionUID = 1L; /** * The name of the service that encountered a fault situation. * * @serial */ @MessageParam("1") private final String serviceName; protected ServiceMessage(final ServiceCode code, final String serviceName) { this(code, null, serviceName); } protected ServiceMessage(final ServiceCode code, final Throwable cause, final String serviceName) { super(code, cause); this.serviceName = serviceName; } }
If the message bean contains information for different contexts, factory methods may help to create specifically filled message bean instances. Be cautions with this technique since it may be confusing if the information in the contexts differ. Clients may easily miss the correct factory.
Be also careful, if you design to create the exception instance within a helper, since the stack trace points to location the exception has been created, not where it has been thrown.
For details on implementing exception codes please refer to the documentation in the smart-exceptions-core project.
Provide a message bundle as for localized messages.
Use de.smartics.exceptions.i18n.message.MessageParam de.smartics.exceptions.i18n.message.ParentMessageParam . But instead of annotating fields of an exception type, as in the case of localized messages, annotate fields of a de.smartics.exceptions.i18n.MessageBean.