Proposed FindBugs pattern

View: New views
1 Messages — Rating Filter:   Alert me  

Proposed FindBugs pattern

by JavaOne :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

// This is an example of a bug I found in code compiled with JDK1.4.  The  
// IOException in JDK1.4 does not have a constructor that takes a cause so 
// the initCause method must be used to chain exceptions.                  
public void example1() throws IOException                                  
{                                                                          
   File.createTempFile( "first", null );                                   
                                                                           
   try                                                                     
   {                                                                       
      File.createTempFile( "second", null );                               
   }                                                                       
   catch ( IOException cause )                                             
   {                                                                       
      IOException ex = new IOException( "Could not create second file." ); 
      ex.initCause( cause );                                               
      // BUG: An instance of Throwable was created but never thrown nor    
      // chained into an instance of Throwable that was thrown.            
      //                                                                   
      // The main reason to create an instance of Throwable is to throw it!
      //                                                                   
      // The only reasonable examples I've found where exceptions are not  
      // thrown is where an exception A is created (but not thrown),       
      // chained into exception B, and then exception B is thrown. I       
      // suppose there are probable examples where a longer chain is built,
      // but eventually the head of the chain should be thrown.            
                                                                           
      // Note that in this case the compiler does not catch the fact that  
      // ex is never thrown since the first call to createTempFile satisfies
      // the throws clause. The bug is also undetected when the exception  
      // that should have been thrown is a RuntimeException.               
   }                                                                       
}
 
Thanks,
 
DS

_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss
LightInTheBox - Buy quality products at wholesale price