IntelliJ Java IDE
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes TryCatchFinally interface.....did you know!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "TryCatchFinally interface.....did you know!!!" Watch "TryCatchFinally interface.....did you know!!!" New topic
Author

TryCatchFinally interface.....did you know!!!

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
When you are writing a custom tag, you can implement TryCatchFinally interface.
When you choose to do this, if the doStartTag() method throws an exception, the doEndTag() method is NOT called. The flow goes to doCatch( ) method, maybe expected right!.
However, if you choose to return "SKIP_PAGE" in the doEndTag( ), you should realize that the page is NOT skipped in the above scenario (since we never went into the doEndTag() ) and that the evaluation of the page continues.
I din't realize this till I played with a real example. Just thought I would pass this along, then again its me.
Learned something new today!!!
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
faiza haris
Ranch Hand

Joined: Oct 17, 2000
Posts: 173
Thanx for sharing...
I also came across something similar just now!!!...
In CS&JSP book, it says that print should be inside try/catch block and to report other types of errors t
o client, u can declare doStartTag() to throw JspException
I dont know about doCatch(). Please state your example if u can? thanx for any help!
faiza
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
In CS&JSP book....
I thought CS&JSP follows the previous specs version. jsp 1.1 and servlets 2.2.
You need MS&JSP which follows jsp 1.2 and servlets 2.3 specs.....
- satya
Rohit Poddar
Ranch Hand

Joined: Aug 18, 2001
Posts: 36
Thanks Satya,
I looked at the TryCatchFinally Interface in API spec. and following is the prototypical invocation :
h = get a Tag(); // get a tag handler, perhaps from pool
h.setPageContext(pc); // initialize as desired
h.setParent(null);
h.setFoo("foo");

// tag invocation protocol; see Tag.java
try {
doStartTag()...
....
doEndTag()...
} catch (Throwable t) {
// react to exceptional condition
h.doCatch(t);
} finally {
// restore data invariants and release per-invocation resources
h.doFinally();
}
Here we see that if any of the doStartTag(), doInitBody(), doAfterBody() or doEndTag() throws any exception and if you have implemented TryCatchFinally, you will recover from failure and will continue with page execution.
Just thought will let people know why the phenomenon you saw was actually happening.
-Rohit

Originally posted by Madhav Lakkapragada:
When you are writing a custom tag, you can implement TryCatchFinally interface.
When you choose to do this, if the doStartTag() method throws an exception, the doEndTag() method is NOT called. The flow goes to doCatch( ) method, maybe expected right!.
However, if you choose to return "SKIP_PAGE" in the doEndTag( ), you should realize that the page is NOT skipped in the above scenario (since we never went into the doEndTag() ) and that the evaluation of the page continues.
I din't realize this till I played with a real example. Just thought I would pass this along, then again its me.
Learned something new today!!!
regds.
- satya


SCJP, SCWCD
 
IntelliJ Java IDE
 
subject: TryCatchFinally interface.....did you know!!!
 
Threads others viewed
exception in doAfterBody
Regarding tags
doEndTag
when do you use doAfterBody() or doEndTag() ??
Tag library ...
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture