File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Handling Exceptions in MVC +Command Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Handling Exceptions in MVC +Command Pattern" Watch "Handling Exceptions in MVC +Command Pattern" New topic
Author

Handling Exceptions in MVC +Command Pattern

faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
Hello..
I am using MVC and Command Pattern for my application.
There are some issues that i want to know.

1)How to handle data base exceptions.
2)what are the best practices to handle server and data base exceptions
thanks
Faisal
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Well, in a strictly MVC application, all database access should be in your model. So, database exceptions should be caught there, and something more meaningful returned to the client.
Best practices ... well I'm not sure--regarding what? This is a pretty vague question. Maybe if you tell us more about what you are wrestling with we can help with something more definite.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
thanks for reply,
I handle all DB exceptions in procedures and functions.
Now tell me what are the possible ways to send the user
friendly message to client(using custom java exceptions,
properties files... or what pattern i will follow which
is more reliable and decouple).
please tell me what are the best choice to handle the exceptions
faisal
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Well, in my opinion you should determine what exceptions are recoverable(application) and what exceptions that are un-recoverable(i.e. server/sytem related).
For example say a user trys to transfer money from on account to another but the transfer amount is more than the current balance.


or say it is server/system type message(unrecoverable) like an SQLException.

As you can see the second method does not expose or mention SQL in the interface. This hides the implementation, thus decouples the interface to a specific mechanism.
I hope this helps and makes some sense.
Craig
faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
thanks..

but one thing, if any exception occurs then how can i send it to the
client(JSP). Because i have if i get exception in command class or

DAO class then according to ur comments i will through it in the business

method.
How controller class will handle it and also in view(JSP).

Also tell me, because i handle all exception in procedures and functions.
so how can i manage that procedure send error code or error message and then
i will send it to the client.
HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
Hi,
You might wish to take a look at how Struts handle exception.
e.g.
catch (DatabaseException ex) {
saveError(ex, errorMessage);
}
say, this store to a request object, maybe "error" map.

Then, in your JSP, you can use something similar to Struts <html:errors />
e.g.
you retrieve the "error" map, and display the error accordingly.
Cheers.

Han Ming
faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
Thanks :roll:
But i do not use struts frame work. So i have no idea about struts fra
me work.
Lets look in to code.
jsp file:

Search user id :

controller servlet:

Create search login command+ call execute method();
Command Execute Method:
execute(){
call oracle procedure which check the user id

if
exesits then return to next jsp: with result set(as
array of bean).
else
send error code (my generated error code)
e.g not exist

}
Now if error occurs then how can i send messgae to the jsp as bean class
or code mapped in property file and then read it in JSP?
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
Look, this is real simple:
Servlet:
doPost(req, res)...{
String forwardTo = "success.jsp"
try{
DataOps dataOps = new DataOps();
dataOps.getStuffFromDataBase();
dataOps.getUserInfoFromLdap(uid);
}
catch(Exception e){
forward = "failure.jsp";
if(e instanceof SQLException){
req.setAttribute("error", "The database is currently being updated. Please try again later");
}
else if(e instanceof NameNotFoundException){
req.setAttribute("error", "The uid entered is incorrect. It does not exist in LDAP!");
}
else{
req.setAttribute("A Service Interuption has occurred!");
}
e.printStackTrace();
}
RequestDispatcher rd = req.getRequestDispatcher(forwardTo);
rd.forward(req, res);
}
Here the method getUserInfoFromLdap throws a NameNotFoundException and getStuffFromDataBase throws a SQLException.
The JSP:
String error = (String)request.getAttribute("error");
if(error != null){
%>
<font color=red>* <%=error%></font>
<%
}
%>
This is the very least that should be done.


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
thanks for reply , now i get the logic.
but one and last thing about the error page,

every exception has same error page ?

thanks
faisal khan
HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
Hi,
I think William has mentioned all the points in his code.
To forward to different jsp, all you need to do is to set the
forward = "failure.jsp";
at each catch statement
say
catch (DbEx ex) {
forward = "p1.jsp";
}
catch (AppEx ex) {
forward = "orig.jsp";
}
catch (SysEx ex) {
forward = "globalp.jsp";
}
I understand that you are not using Struts, but Struts does give you many ideas to the solution which I believe you will use in many web app dev.
So, if you have the time, I encourage you to pick up a book and read on it.
Moreover, the struts code is FREE.
It's the idea that you need to understand, whether you will be using struts or not is not important.
Cheers.

Han Ming
faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
thanks to all.
Faisal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling Exceptions in MVC +Command Pattern
 
Similar Threads
How to practice the web tire design patterns?
Exception handling patterns?
storing object in servletcontext
Handling Threading Issue ???
MVC Commands