Ask Experts Questions for FREE Help !
Ask
    10981632's Avatar
    10981632 Posts: 2, Reputation: 1
    New Member
     
    #1

    Nov 9, 2014, 10:53 AM
    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/
    10981632's Avatar
    10981632 Posts: 2, Reputation: 1
    New Member
     
    #2

    Nov 9, 2014, 11:00 AM
    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-

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Error in getting address of sender of mail using javamail api [ 0 Answers ]

I am getting address of sender of any mail like this "Ljavax.mail.internet.Address@jkkj5j" when I use "message.getFrom()" and connecting to store using imap protocol. Can anyone help me please? Thanks in advance.

API Error Message [ 4 Answers ]

Hello everyone.. While attempting to play a few games today, I encountered this error message: "Must call RA_Initialize before any other API function." I received this error message while trying to play games from Reflexive Arcade. I can download the games just fine.. and it'll put an...


View more questions Search