10.4 DIFFERENCES BETWEEN C AND JAVA FOR EXCEPTION HANDLING


10.4 DIFFERENCES BETWEEN C++ AND JAVA FOR EXCEPTION HANDLING

There are important differences between C++ and Java with regard to how exceptions are handled and processed.[06] These arise on account of the following:

  1. While literally any type can be used for throwing in C++, that's not the case in Java. An object that's used for throwing in Java must of type that is a subclass of Throwable. By convention, however, user-defined exceptions are of type Exception, a subclass of Throwable. Therefore, if we wish to write a Java method that throws an exception of a user-defined type MyException, we must define MyException by extending Exception, as in

          class MyException extends Exception { ...... } 
  2. If a function is written so as to throw an exception, that fact must be declared in the header of the function. So if a function f() is written to throw an exception of type MyException, we have no choice but to include the throws MyException clause in the header of the function, as in

          void f(int j) throws MyException {         // .....         throw new MyException (// .....);      } 

    Also note that, as the reader should expect by this time, the use of the keyword new for constructing an object of type MyException.

  3. An exception-throwing function can only be invoked in one of the following modes:

    • It must be invoked inside a try-catch block and the exception must be caught; or

    • The calling function must re-throw that exception so that it can be caught elsewhere, possibly by the system-supplied default exception handler.

    • The calling function can do both of the above.

    It goes without saying that the behavior of a program with regard to exception throwing and catching will be different depending on which mode is chosen.

  4. Another important difference between the Java syntax and the C++ syntax is that a parameter must be explicitly specified for the catch block. In other words, one now has no choice but to say

           catch(MyException e) { .... } 

    and explicitly mention an identifier, in this case e of type MyException in the parameter list of catch () even if this parameter does not get used for anything inside the definition of the catch block.

We will now show the code for a Java implementation of the C++ example of Section 10.2.

 
//TryCatch. java import java.io.*; class Err extends Exception { } class Test { public static void main(String [] args) { try { f(0); } catch (Err e) { System. out. println( "Exception caught in main" ); } } static void f(int j) throws Err { System.out.println( "function f invoked with j = " +j ); if (j == 3) throw new Err(); f (++j); } }

Note that the header of the function f includes the clause throws Err. The output produced by this program is

      function f invoked with j = 0      function f invoked with j = 1      function f invoked with j = 2      function f invoked with j = 3      Exception caught in main 

[06]The discussion in this and the next two sections applies only to what are known as checked exceptions in Java-the kind of exceptions you are most likely to incorporate in your own programs. The difference between Java's checked and unchecked exceptions is explained in Section 10.7.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net