User Guide

Table Of Contents
936 Chapter 37: Integrating J2EE and Java Elements in CFML Applications
The JavaCast function takes two parameters: a string representing the Java data type, and the
variable whose type you are setting. You can specify the following Java data types: boolean, int,
long, float, double, and String.
For more information about the
JavaCast function, see CFML Reference.
Handling Java exceptions
You handle Java exceptions just as you handle standard ColdFusion exceptions, with the
cftry
and
cfcatch tags. You specify the name of the exception class in the cfcatch tag that handles the
exception. For example, if a Java object throws an exception named myException, you specify
myException in the
cfcatch tag.
Note: To catch any exception generated by a Java object, specify java.lang.Exception for the cfcatch
type attribute. To catch any Throwable errors, specify java.lang.Throwable in the cfcatch tag type
attribute.
The following sections show an example of throwing and handling a Java exception.
For more information on exception handling in ColdFusion MX, see Chapter 14, “Handling
Errors,” on page 307.
Example: exception-throwing class
The following Java code defines the testException class that throws a sample exception. It also
defines a myException class that extends the Java built-in Exception class and includes a method
for getting an error message.
The myException class has the following code. It throws an exception with a message that is
passed to it, or if no argument is passed, it throws a canned exception.
//class myException
public class myException extends Exception
{
public myException(String msg) {
super(msg);
}
public myException() {
super("Error Message from myException");
}
}
The testException class contains one method, doException, which throws a myException error
with an error message, as follows:
public class testException {
public testException ()
{
}
public void doException() throws myException {
throw new myException("Throwing an exception from testException class");
}
}