
Using Java objects
753
The ColdFusion
JavaCast
function helps you resolve such issues by specifying the Java type of a
variable, as in the following line:
<cfset emp.SetJobGrade(JavaCast(“int”, “1”))>
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 285
.
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");
}
}
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......