Catching Exceptions
There are three general ways that you can code your JSP pages to catch exceptions:
Use an error page.
Use a try/catch block.
Use the unit tag and nested success and failure tags (not suggested in JSPs).
Using an Error Page
By creating an error page and specifying it in the page directive on your JSP pages, you can catch exceptions from the page. You can designate that a page is an error page on the page directive by setting the isErrorPage attribute to true. For example:
<%@page language="java" isErrorPage="true"
import="java.io.*,com.infoengine.util.*,com.infoengine.exception.*,
com.infoengine.exception.fatal.*,com.infoengine.exception.nonfatal.*,
java.util.Hashtable"%>
Use your error page by specifying it as the error page in the page directive on other pages. For example the following page directive sets the errorPage attribute to IEError.jsp:
<%@page language="java" session="false" errorPage="IEError.jsp"%>
An error page can be very useful because it can act as a common location for error processing to give a consistent experience when errors occur. An error page can also be written to deal with exceptions based on their type, presenting more meaningful information to the user.
Using try/catch Blocks
You can catch exceptions within specific parts of a JSP by using try/catch blocks within scriptlets. This is similar to handling exceptions using unit and failure tags, but allows you to more programmatically deal with exceptions. For example:
<%
try {
%>
<!-- webject or task invocations -->
<%
// catch statement appropriate given the contents of this block
} catch ( AdapterException ae ) {
//handle the adapter exception
} catch ( IDPartialResultsException partial ) {
// extract and display the partial results
}
// etc.
%>
Using unit Tags
You can catch exceptions within specific parts of a page by using unit tags on the page and including one or more failure blocks within the unit. In the previous section, the failure block provided was for all failures. In addition to having a general failure block, you can have failure blocks that catch a specific exception. The following failure blocks catch four different exceptions:
<ie:failure exception="AdapterException">
<ie:webject name="Create-Group" type="GRP">
<ie:param name="ELEMENT"data="FAILURE=AdapterError"/>
<ie:param name="DELIMITER"data=":"/>
<ie:param name="GROUP_OUT"data="failure"/>
</ie:webject>
<ie:webject name="Object-XML" type="DSP"/>
</ie:failure>

<ie:failure exception="IEPartialResultsException">
<ie:webject name="Create-Group" type="GRP">
<ie:param name="ELEMENT"data="FAILURE=PartialResults"/>
<ie:param name="DELIMITER"data=":"/>
<ie:param name="GROUP_OUT"data="failure"/>
</ie:webject>
<ie:webject name="Object-XML" type="DSP"/>
</ie:failure>

<ie:failure exception="IEInternalServiceException">
<ie:webject name="Create-Group" type="GRP">
<ie:param name="ELEMENT"
data="FAILURE=InternalServiceError"/>
<ie:param name="DELIMITER"data=":"/>
<ie:param name="GROUP_OUT"data="failure"/>
</ie:webject>
<ie:webject name="Object-XML" type="DSP"/>
</ie:failure>

<ie:failure exception="IEFatalException">
<ie:webject name="Create-Group" type="GRP">
<ie:param name="ELEMENT"data="FAILURE=FatalException"/>
<ie:param name="DELIMITER"data=":"/>
<ie:param name="GROUP_OUT"data="failure"/>
</ie:webject>
<ie:webject name="Object-XML" type="DSP"/>
</ie:failure>
You can also use the Throw-Exception webject to throw your own exceptions or rethrow an exception from within a page or a task. All exceptions thrown during task webject execution are automatically entered in the SERVER context group as the attributes named exception-class and exception-message. Therefore, you can use the Throw-Exception webject to rethrow and exception and its message without knowing what the exception is.
Было ли это полезно?