HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Exception Handling in Java:
How to Use Exceptions Effectively"
Vol. 2, Issue 9, p. 52

	

Listing 1: C++ Exceptions and Memory Management.
 
//Function WillLeakMemory 
void  WillLeakMemory () { 

  //allocate some resources dynamically 
  fstream* ptrFile = new fstream(); 
  DatabaseConnection* ptrDB = new DatabaseConnection(); 
  //attempt to open Database Connection - can throw an exception 
  ptrDB->Open(); 

  //do some work 

  //close connection and delete resources 
  ptrDB->Close(); 
  delete ptrDB; 
  delete ptrFile; 
} 

Listing 2. C++ exception specifications are not checked at compile-time.
 
//Note: this code will compile with no errors 
//Exception class 
class Exception { 
public: 
  Exception(): strError("") {}; 
  Exception( const char* strErrorText ) : strError( strErrorText ) {}; 
  const char* GetErrorText() const { return strError; }; 
private: 
  const char* strError; 
}; 

//Function foo 
//throws exception despite of its specification 
void foo() throw(){  
  throw(Exception( "hey")); 
} 

//main - call foo 
main() { 
  foo(); 
  return 1; 
} 

Listing 3.
 
public class foo 
{ 
  public void doSomeWork() 
  { 
    try 
    { 
      //do some work 
    } 
    finally 
    { 
      //clean-up the vector 
      fooVector.removeAllElements(); 
    } 
  } 
  private myVector fooVector; 
} 

Listing 4: Overriding Methods and Exception Specifications
 
//myException class 
class myException extends Exception 
{ 
  myException()  
  { 
    super();  
  } 
  myException( String strErrorText ) 
  { 
    super( strErrorText ); 
  } 
 } 

//myTestSuper 
class myTestSuper 
{ 
  public void DoSomeWork1()  
    throws SQLException, myException 
  { 
    //do some work 
  } 
  public void DoSomeWork2()  
    throws myException 
  { 
    //do some work 
  } 
} 

//myTestChild - overrides both methods of myTestSuper 
class myTestChild extends myTestSuper 
{ 
  public void DoSomeWork1()  
    throws myException //will compile O.K. 
  { 
    //do some work 
  } 
  public void DoSomeWork2()  
    throws SQLException //will result in compile error 
  { 
    //do some work 
  } 
} 

Listing 5.
 
interface A{ 
  public void Work() throws SQLException; 
} 
interface B { 
  public void Work() throws SQLException, ClassNotFoundException; 
} 
class Impl implements A,B { 
  public void Work() throws ClassNotFoundException //Error!!! 
  { 
    //do some work 
  } 
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.