Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Catch-or-declare error but exception not shown in the API (https://www.askmehelpdesk.com/showthread.php?t=804274)

  • Nov 9, 2014, 10:53 AM
    10981632
    Catch-or-declare error but exception not shown in the API
    In the Java API documentation the constructor for FileInputStream may throw an exception when called. According to the documentation, this exception is of class FileNotFoundException. There are others listed, but these are runtime exceptions which are handled differently.


    Why does the compiler generate an catch-or-declare error for exception IOException even when FileNotFoundException is caught or declare?


    For example, the following code will not compile, even though FileNotFoundException is caught:


    Code:

                    FileSystem system=FileSystems.getDefault();
                    Path path=system.getPath("c:\\users\\eu-A\\ToDoList.csv");
                    File file=path.toFile();
                    if(!file.exists()) {
                            try {
                                    file.createNewFile();
                            }
                            catch(IOException e) {
                                    System.exit(-1);
                            }
                    }
                    else {
                            if(file.canRead()) {
                                    try(FileInputStream stream=new FileInputStream(file)) {
                                            String value="";
                                            int b;
                                            do {
                                                    b=stream.read();
                                                    value+=(char) b;
                                            } while(b>0);
                                            stream.close();
                                    }
                                    catch(FileNotFoundException e) {
                                            System.exit(-1);
                                    }
                            }
                    }

    The reason for this is that only FileNotFoundException is caught- but IOException is neither caught nor declared.


    Why is this the case when the API documentation lists only FileNotFoundException as throwable when calling this constructor (besides the runtime exceptions which are handled differently)?


    Is it my understanding of how the API documentation is presented that is at fault?


    Are all exceptions that can possibly be thrown by a method given in the method's description in the API documentation?


    Here is the link to the relevant section of the documentation:


    https://docs.oracle.com/javase/8/docs/api/
  • Nov 9, 2014, 11:00 AM
    10981632
    My apologies- Catch-or-declare error but exception not shown in the API
    The link to the relevant section in the API documentation is as follows:

    https://docs.oracle.com/javase/8/docs/api/java/io/FileInputStream.html#FileInputStream-java.io.File-

  • All times are GMT -7. The time now is 02:03 PM.