Calling Java Objects
Example: exception-throwing class
The following Java code defines the foo class that throws a sample exception. It also
defines a fooException class that extends the Java built-in Exception class and
includes a method for getting an error message.
public class foo {
}
Class fooException
public class fooException extends Exception {
}
Example: CFML Java exception handling code
The following CFML code calls the foo class doException method. The
handles the resulting exception by calling the CFML
retrieve the Java exception object and then calling the object's GetErrorMessage
method to get the error information.
<cfobject action=create type=java class=foo name=Obj>
<cftry>
</cftry>
Reviewing the code
The following table describes the code and its function:
Code
<cfobject action=create type=java
class=foo name=Obj>
<cftry>
<cfset VOID = Obj.DoException() >
public foo() {
}
public void doException() throws fooException {
throw new fooException("I am throwing a throw Exception ");
}
public String GetErrorMessage() {
return "Error Message from fooException";
}
<cfset VOID = Obj.doException() >
<cfcatch type="Any">
<cfset exception=GetException(Obj)>
<cfset message=exception.GetErrorMessage()>
<cfoutput>
<br>The exception message is: #message#<br>
</cfoutput>
</cfcatch>
GetException
Description
Load an instance of the Java foo class
named Obj.
Inside a cftry block. call the doException
method of the Obj object. This method
throws an exception of the fooException
type.
383
block
cfcatch
function to
Need help?
Do you have a question about the COLDFUSION 5-DEVELOPING and is the answer not in the manual?
Questions and answers