• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Study kit example doubt

 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
)In Study kit,they have mentioned an example as below.I find everything fine in this,but shouldn�t it have a throws InsufficientFundsException in the doPost method of servlet which is not handling the exception there?


Q: In section 7.2.1, we discussed an imaginary account servlet that handles
InsufficientFundsException programmatically. How will you
implement the servlet using declarative exception handling?
A: Step 1: Extend the InsufficientFundsException from Runtime-
Exception and let it be thrown outside the servlet, as shown here:
public void doPost(HttpServletRequest req,
HttpServletResponse res)
{
String command = req.getParameter("command");
if("debit".equals(command))
{
double amount =
Double.parseDouble(req.getParameter("amount"));
String accountId =
req.getSession().getAttribute("accountId");
//do not handle the exception thrown by debit()
double newBalance = debit(accountId, amount);
//generate HTML page showing new balance.
}
else
{
//do something else
}
}
Step 2: Define the mapping of this exception to an error page in the
web.xml file:
<error-page>
<exception-type>InsufficientFundsException</exception-type>
<location>/servlet/BusinessLogicExceptionHandlerSerlvet</
location>
</error-page>
Step 3: Implement the exception handler to generate an appropriate
message. The following is a sample implementation of the service()
method of the exception handler servlet:
public void service(HttpServletRequest req,
HttpServletResponse res)
{
PrintWriter pw = res.getWriter();
pw.println("<html><head></head><body>");
Throwable exception = (Throwable)
req.getAttribute("javax.servlet.error.exception");
if(exception instanceof InsufficientFundsException)
{
pw.println("<h3>Sorry, you do not have
enough funds in your account!</h3><p>");
pw.println("<h4>Current balance :"+
((InsufficientFundsException) exception).amtInAcc+
"</h4><p>");
pw.println("<h4>Amount :"+
((InsufficientFundsException) exception).amtTobeWithdrawn +
"</h4><p>");
}
else
{
//do something else
}
pw.println("</body></html>");
}
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but shouldn�t it have a throws InsufficientFundsException in the doPost method of servlet

No, it's a RuntimeException. Moreover, doPost is managed by the container, and part of the contract defined in the servlet specification. You can't change its signature.
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does all user defined exception extends from runtime exception?
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess all userdefined exceptions are checked exceptions.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, user made exception created by extending Exception class creates checked exception.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does all user defined exception extends from runtime exception?

No. As you said, user exceptions will be (most of the time) checked exception, but you can't throw them from a doPost. Who would catch it ?
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok..I got it now.The doPost method pass it to the container and the container looks in the DD for the page mapped to this exception and shows that.Thanks Santou
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic