Internet Explorer 5.0 Review, Part IV: Exception Handling, Comparison with Java & C++ - www.docjavascript.com | WebReference

Internet Explorer 5.0 Review, Part IV: Exception Handling, Comparison with Java & C++ - www.docjavascript.com


Internet Explorer 5.0 Review, Part IV: Exception Handling (5)

Comparison with Java and C++

Not surprisingly, the exception-handling syntax in C++ and Java is very similar to JavaScript's. As in JavaScript, it also centers around the try..catch statement. The main difference is in the catch's argument. As explained in a previous page, this argument can be of any data type in JavaScript. It can be an object, a string, an integer, or any other supported type. In fact, JavaScript does not support type declaration, so there is no way to declare the type of the catch's argument. In Java, the catch's argument is an instance of a Java class that extends the standard Java Throwable class. The catch statement would look like this:

catch (objectType exception)

where exception is an object of the type objectType. C++ is not that restrictive. The catch's argument can be an instance of the standard C++ exception class, or it can be of any C++ type:

catch (char *message)

Since there is no type declaration in JavaScript, it supports only one catch block per try block. As shown in a previous page, whenever our exception handling depends on the argument type, we need to check if it is of the right type via the instanceOf function. In C++ and Java, you can use multiple catch blocks per a single try block. Each catch block handles exceptions of the proper type. Suppose you want to handle differently exceptions of type char and of type myException:

try {
   // following function can throw exceptions of type char or myException
   doSomeThingUseful();
}
catch (char *exceptionString) {
   // handle the case  that exception is a string
}
catch (myException exceptionObject)
   // handle the case that exception is an instance of type myException
}

In JavaScript you would do something along these lines:

try {
   // following function can throw exceptions of type char or myException
   doSomeThingUseful();
}
catch (exception) {
   // verify exception is of myExceptionObject type
   if (exception instanceOf myExceptionObject) {
     if (exception.kind == "string") {
       // handle the sting exception
     }
     else if (exception.kind == "myException") {
       // handle the object exception
     }
}

Another difference is that Java and C++ supports exception catch-all, i.e. catching all exception in one catch block. The syntax in Java is:

catch (Throwable t) {
  // Handle all cases
}

where Throwable is a predefined Java class. The equivalent catch-all block in C++ is:

catch ( ... ) {
  // Handle all cases
}

As shown above, since JavaScript supports a single catch block anyway, all exceptions are caught by the same catch block by definition. You would use:

catch ( exception ) {
  // Handle all exceptions
}

Similar differences also exist between the throw statements of C++, Java, and JavaScript. The syntax is identical among three languages:

throw exceptionObject;

As Java can catch only Throwable types, exceptionObject must be of this same type. C++ and JavaScript can throw any data type, including strings, integers, objects, etc.

C++ and Java differ from JavaScript in the way exceptions propagate upward. If an exception is not handled in C++ or Java, it is automatically promoted to a higher level in the calling scheme, i.e. to the caller function, the main program, or the browser. In JavaScript, you have to promote the exception yourself if you want a higher level to handle the exception. As shown in a previous page, you would use the simple syntax:

throw exceptionObj;

The last difference is that C++ and Java do not support nesting of try..catch blocks. In JavaScript, though, you can embed a try...catch statement within a try or a catch block of another statement.

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: April 26, 1999
Revised: April 26, 1999

URL: http://www.webreference.com/js/column38/java.html