home / experts / javascript / column38 |
|
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
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++
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
In JavaScript you would do something along these lines:
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:
where
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:
Similar differences also exist between the throw statements of C++, Java, and JavaScript. The syntax is identical among three languages:
As Java can catch only 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:
The last difference is that C++ and Java do not support nesting of |
Produced by Yehuda Shiran and Tomer Shiran
Created: April 26, 1999
Revised: April 26, 1999
URL: http://www.webreference.com/js/column38/java.html