Wiki
Clone wikiorg.openmarkov / Developing_code / OpenMarkov_exceptions
OpenMarkov Exceptions
{Merge and distribute with localization}
OpenMarkov exceptions use a specific frame to display a Swing error message and to access the text that should go into that message.
Imagine that you catch a NullPointerException
and you want to tell the user that there is no free lunch:
Modyfing the text for the exception
- Add the text you want to show the user (title and message) to the xml string database that adresses localization:
- In the plugin package that throws the exception
- In the gui package otherwise
- The exception should be formatted as it's here:
Note that the token of this exception will be the list of enclosing fields separated by dots:
<Reality> <NoFreeLunch> <title value="No free lunch"/> <message value="Nothing is free and lunch above everything"/> </NoFreeLunch> </Reality>
Reality.NoFreeLunch
Creating the exception
- Now create an
OpenMarkovException
with the token as argument. - Wrap this exception by a
LocalizedException
with theOpenMarkovException
as first argument and the parent GUI component as second (you don't throw the localized exception) - The
LocalizedException.showException()
method will show an error message with the title and the body (message) from the xml.
The complete java code for the example above is:
catch(NullPointerException e) { LocalizedException NoFreeLunchException = new LocalizedException(new OpenMarkovException("Reality.NoFreeLunch"), this); NoFreeLunchException.showException(); }
Formatted message
OpenMarkovException
formats strings the same way than System.out.printf
does: OpenMarkovException("No %s, then no %s, "lunch", "work")
. But it only allows string arguments.
The placeholder allow different commands:
%-5s
%
Beginning of format
-
OPTIONAL. Use left-justification on minimal whitespaces. See below.
5
Minimum whitespaces for the print. Right-justifies. Empty means 0.
s
Type code (string coerced)
Updated