Bug 46421

Summary: Compiler doesn't require 'throws' declaration when class throws IOException
Product: [Eclipse Project] JDT Reporter: Philip Gust <gust>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: RESOLVED DUPLICATE QA Contact:
Severity: major    
Priority: P3    
Version: 3.0   
Target Milestone: 3.0 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Philip Gust CLA 2003-11-11 13:01:20 EST
On Version: 3.0.0 Build id: 200310101454 the incremental compiler does not 
require me to add a 'throws' declaration when my method catches and rethrows a 
java.io.IOException.  With the appropriate options enabled, the IDE also 
reports a warning when I add the 'throws' declaration that the declared 
IOException is not actually thrown by the method.  Sun's javac flags the 
missing 'throws' declaration as an error. I've marked this 'major' because it 
compromises the integrity of compile-time verification.

Here is the method:

        private byte[] getSomeBytes(InputStream in, long length) 
            throws IOException
        {
            DataInputStream din = new DataInputStream(in);
            byte[] bytes = new byte[(int)length];
            try {
                din.readFully(bytes);
            } catch (IOException ex) {
                MessageHandler.error(ex);  // log exception
                throw ex;
            } finally {
                return bytes;
            }
        }
Comment 1 Olivier Thomann CLA 2003-11-11 13:07:14 EST
In fact this is not a problem. You really don't need the throw clause, because
your method will never throw the exception even if you go in the catch block.
The reason is that your finally block always returns an array of bytes and
silently consume the potential IOException.
Comment 2 Olivier Thomann CLA 2003-11-11 13:10:27 EST
Looks like a duplicate of bug 22445.
Comment 3 Olivier Thomann CLA 2003-11-11 15:40:11 EST
Close as duplicate of bug 22445.

*** This bug has been marked as a duplicate of 22445 ***