Exceptions Raised in Java™ Objects Invoked From ASP

J-Integra® transparently maps Java™ exceptions to COM exceptions.

When you invoke a Java method from your ASP page, and that method throws an exception, J-Integra® will catch the exception and translate it into an appropriate COM exception.

As an example, create the following Java code in a file called ExceptionDemo.java.

public class ExceptionDemo {
  public void doSomething() throws Exception {
    System.out.println("doSomething() called, throwing exception");    
    throw new Exception("doSomething() is having a bad day");
  }

  public void pleaseDoSomething() throws com.linar.jintegra.AutomationException {
    System.out.println("pleaseDoSomething() called, throwing exception");    

    long code = 0x80020009;
    String source = "The source of the exception.";
    String description = "The description of the exception.";
    throw new com.linar.jintegra.AutomationException(code, source, description);
  }
}

Compile the code and launch DCOMBridgeJvm as you did in the Simple Java Class Example.

Now, create the following ASP script and make it available via IIS.

<%@ LANGUAGE = VBScript %>
<HTML>
<HEAD>
<TITLE>Accessing Java classes from ASP - How to handle Java exceptions</TITLE>
<BODY>
<%
	Set Demo =  GetObject("aspjvm:ExceptionDemo")
	Response.write("About to call Demo.doSomething() which throws a java.lang.Exception.<BR><BR><HR>")
	On Error Resume Next
	Demo.doSomething()
	if Err <>  0 then
		Response.write("Err is: '" & Err & "'<BR>")
		Response.write("Err.Number is: '" & Err.Number & "'<BR>")
		Response.write("Err.Description is: '" & Err.Description & "'<BR>")
		Response.write("Err.Source is: '" & Err.Source & "'<BR>")
	Else
		Response.write("Demo.doSomething() did not throw an exception.<BR>")
	End if
	Response.write("<BR><HR><BR><BR>")
	Response.write("About to call Demo.pleaseDoSomething() which throws a com.linar.jintegra.AutomationException.<BR><BR><HR>")
	On Error Resume Next
	Demo.pleaseDoSomething()
	if Err <> 0 then
		Response.write("Err is: '" & Err & "'<BR>")
		Response.write("Err.Number is: '" & Err.Number & "'<BR>")
		Response.write("Err.Description is: '" & Err.Description & "'<BR>")
		Response.write("Err.Source is: '" & Err.Source & "'<BR>")
	Else
		Response.write("Demo.pleaseDoSomething() did not throw an exception.<BR>")
	End if
	Response.write("<BR><HR>")
%>
</BODY>
</HTML>

When you access the page, you should see the following output.

This demonstrates how you can handle exceptions thrown from any Java code, even if you don't have the source.It also demonstrates how to pass specific error information from your Java code back to the ASP script, enabling you to build intelligent error handling into your application.

Notice the stack trace in the source field, where the java.lang.Exception was thrown. This is very useful information when debugging your Java code.