public class ApplicationException extends Exception {
public ApplicationException(String message) {
super(message);
}
}
. . .
public class SystemException extends Exception {
public SystemException(String message) {
super(message);
}
}
. . .
(typical manager -- facade, middle-tier gateway, etc. -- method):
public void logon(User user) throws ApplicationException, SystemException {
// . . .
}
. . .
System exceptions are for indicating problems such as network failure, JDBC
problems, etc. Application exceptions are for indicating user error such as
wrong password, etc. Note that the method body of the "logon" method (any
backend methods invoked by the method) could throw any subclass of
ApplicationException or SystemException. This keeps the interface simple but
adds a lot of power.
In my Struts config file, I declare a separate message resources bundle that
contains nothing but messages for System and Application exceptions -- a
separate message for each possible subclass (parameters such as "type" is
another way to handle this, but I like real types vs parameters).
Now, your Action's execute method does *not* catch ApplicationException or
SystemException -- it lets them go. Meanwhile, in each action mapping in your
config file, you configure a path and a message (which comes from the resource
bundle) for each possible exception type that could be thrown by the relevant
manager(s). Or, you declare a dedicated exception handler for each type (or one
for System, one for Application), and let that guy determine paths, find
messages, etc. Either way, you can get away with only declaring one for
SystemException and one for ApplicationException, and fill in subclass handling
later if you need. Struts underneath matches exception types going from
specific to general.
. . .
First way:
<action path="/person/logon" type="LogonAction" name="LoginForm"
scope="request" validate="true" parameter="doLogon" input="/logon.jsp">
<exception key="logon.error.unknownUser" type="UnknownUsernameException"
bundle="ExceptionMessages"/>
<exception key="logon.error.authFailure" type="AuthenticationException"
bundle="ExceptionMessages"/>
<exception key="logon.error.accountInactive"
type="InactiveAccountException" path="/logon.jsp" bundle="ExceptionMessages"/>
<forward name="success" path="/home.jsp"/>
<forward name="failure" path="/error.jsp"/>
</action>
Second way (dedicated Exception handler):
<action path="/log_on" type="LogonAction" parameter="doLogon"
name="logonForm" validate="false" scope="request">
<exception key="" type="SystemException"
handler="SystemExceptionHandler"/>
<exception key="" type="ApplicationException"
handler="SystemExceptionHandler"/>
</action>
. . .
Basically what you are doing here is making Exception handling declarative
instead of programmatic. Once you have the components in place, with each new
Exception type, you only need to add messages, etc. No new code (except for
possibly the new Exception type). Note that a logon failure could be caused by
many different things. Here you have a way to customize messages for each case
without adding separate handlers (or even catch blocks) for each case.
Hope that helps,
Erik
-----Original Message-----
From: Rivka Shisman <[EMAIL PROTECTED]>
Sent: Jul 19, 2005 3:37 AM
To: Struts Users Mailing List <[email protected]>
Subject: ApplicationException sample
Hi all,
Can someone please attach a sample of an ApplicationException,
BaseException & SystemException classes?
An example of catching them in an Action class will be very helpful too.
Thanks a lot
Rivka
**************************************************************************************************
The contents of this email and any attachments are confidential.
They are intended for the named recipient(s) only.
If you have received this email in error please notify the system manager or
the
sender immediately and do not disclose the contents to anyone or make copies.
** eSafe scanned this email for viruses, vandals and malicious content. **
**************************************************************************************************
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]