The Mozilla
Organization
Our Mission
Who We Are
Getting Involved
Community
Editorials
What's New
Development
Roadmap
Module Owners
Blue Sky
Projects
Status
Tools
Products
Source Code
Binaries
Documentation
License Terms
Bug Reports
Search
Feedback


LiveConnect Exceptions
by Chris Cooper <coop@netscape.com>

LiveConnect enables communication between JavaScript and Java applets in a page and between JavaScript and plug-ins loaded on a page. However, until now, Java and JavaScript were unable to catch exceptions generated by the other language. The implementation of LiveConnect exception handling now makes graceful error recovery between the two languages possible.

Get there quick:


Catching Java exceptions in JavaScript

Java exceptions are throw back into JavaScript when a line of JavaScript code tries to access a Java member variable or method and fails.

eg. the following example JavaScript code...
try {
        java.lang.Class.forName("blah");
} catch (e) {
        print("Java Exception caught: " + e);
}
        

...would catch something like this:

Java Exception caught: java.lang.ClassNotFoundException: blah


Catching JavaScript exceptions in Java

There are two instances where this might occur:

  1. A Java object successfully evaluates a line of Javascript code that contains a catch() clause that is activated by JavaScript code. In this case, a simple JSException is created and passed back to Java. This JSException contains all the information relevant to the exception.

  2. A Java object attempts to evaluate a line of Javascript, but fails for some reason (syntax, etc.). In this case, the Java exception that is generated is wrapped in an native jobject while in JavaScript and is unwrapped as the original Java exception once it is passed back into Java.

Both of these types of exceptions can be caught in the same way:

eg. the following example Java code...
try {
	obj.eval(code);
} catch (JSException e) {
	if (e.getWrappedException() == null)
                return e;
        return e.getWrappedException();
}
        

...would catch something like this...

java.lang.UnsatisfiedLinkError: eval

...depending on the 'code' it was passed.


Examples

The following two files can be used to test the various features of LiveConnect exception handling.

Download: (Use RightClick-"Save Link As...")

test.js
Test.java

Below is a listing of both files, followed by a listing of the output generated when the files are run through the LiveConnect engine (jsj).

test.js
try {
        supermario();
} catch (e) {
        print("JS Exception caught: " + e);
}

try {
        java.lang.Class.forName("blah");
} catch (e) {
        print("Java Exception caught: " + e);
}

try {
        str = Packages.Test.doit(this, "throw 'foo';", true);
        print(str);
} catch (e) {
        print("Wrapped Exception caught: " + e);
}
	    
Test.java
import netscape.javascript.JSObject;
import netscape.javascript.JSException;

public class Test {

    public static Object doit(JSObject obj, String code, boolean catchit) {
        if (catchit) {
            try {
                obj.eval(code);
            } catch (JSException e) {
	        if (e.getWrappedException() == null)
                        return e;
                return e.getWrappedException(); 
            }
        } else {
            obj.eval(code);
        }
        return null;
    }
}
	    
Output:

[user@host liveconnect]$ Linux_All_DBG.OBJ/jsj test.js
JS Exception caught: ReferenceError:test.js:2: supermario is not defined
Java Exception caught: java.lang.ClassNotFoundException: blah
Wrapped Exception caught: java.lang.UnsatisfiedLinkError: eval
[user@host liveconnect]$

Troubleshooting

  • make sure your source tree, specifically the LiveConnect engine and source, are up to date
  • make sure your netscape.javascript classes are up to date, compiled, and in your $CLASSPATH
  • make sure Test.java is compiled and in your $CLASSPATH


Chris Cooper
Last modified: Fri Oct 9 17:22:31 EDT 1998



Copyright © 1998 The Mozilla Organization.