This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Study kit example doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Study kit example doubt" Watch "Study kit example doubt" New topic
Author

Study kit example doubt

Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
)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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
So does all user defined exception extends from runtime exception?
Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
I guess all userdefined exceptions are checked exceptions.
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Yes, user made exception created by extending Exception class creates checked exception.


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Oct 21, 2005
Posts: 243
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Study kit example doubt
 
Similar Threads
Code Review - ChatServlet
Servlet
Study kit-error attributes
prb from Hemant Deshmukh...
Customerized exception handling