Results 1 to 12 of 12
  1. #1
    gcampton Guest

    Default can I do this, throwing fnfe exception on class constructor?

    I have been trying to think of the best way to implement a MyFile_IO class.
    And would like to send the filename to the class when invoked as an object, and for the class to test if the file exists, But I am unsure as to the extent of exception handling. Do throws run checks or do they simply sit there and only work if the VM stumbles across the errors. Ok my code might make some more sense of what I am trying to accomplish.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class My_FileIO
    {
        private String fileName;
        public My_FileIO( String fName) throws FileNotFoundException 
        {
            setFileName( fName );
        }
        public void setFileName( String fileName )
        {
            this.fileName=fileName;
        }
        public String getFileName() ...
            ...
        public void openFile() ...
    }
    So basically simply setting of a filename to a method, will throwing FileNotFoundException check to see if the file exists? or will i need to create a new Scanner(new File( fileName )) for the actual exception handling (checking to see if case exists) to take place?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    There is no substitute for reading the tutorials.

  3. #3
    gcampton Guest

    Default

    Hmmm doesn't look much different to the chapter in my book, it has a lot of conditional statements followed by throws inside try catch blocks....

    So i'm guessing the answer is NO it simply creates an exception object, so I need conditional.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You also need to read the API specs for the File class.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    The "throws" clause in the method/constructor declaration only tells other methods that call it that that exception might result and that they need to handle that.

    The method that declared throws, then either need to do something that might result in that exception, such as attemtping to open a FileInputStream to a File that doesn't exist, or must manually throw the Exception using the throw(Throwable) method, to actually cause that exception.

    Simply declaring that the method throws an Exception does not do anything. It is simply a "warning" to anything calling it that that exception might result.

  6. #6
    gcampton Guest

    Default

    Quote Originally Posted by r035198x View Post
    You also need to read the API specs for the File class.
    for the ioe and fnfe?

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by gcampton View Post
    for the ioe and fnfe?
    The first bits of your question were nibbled off by the forum goblins.

  8. #8
    gcampton Guest

    Default

    Quote Originally Posted by masijade View Post
    Simply declaring that the method throws an Exception does not do anything. It is simply a "warning" to anything calling it that that exception might result.
    Thank you for the expansion, that was what I was unsure of, but probably should have guessed considering the all the random examples use made up:
    if (num == 0) throw new zeroNumberException

  9. #9
    gcampton Guest

    Default

    Quote Originally Posted by r035198x View Post
    The first bits of your question were nibbled off by the forum goblins.
    FileNotFoundException & IOException

    Have been taught that as a standard you should use only the abbreviated Capitals for naming convention.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    So are you asking if you should read their specs too?
    Well it doesn't hurt.

  11. #11
    gcampton Guest

    Default

    Quote Originally Posted by r035198x View Post
    So are you asking if you should read their specs too?
    Well it doesn't hurt.
    I'm sorry because I know you are trying to help but, you sound like your posting angry.

    I asked a question on exception handling that I was uncertain about from reading the text book and the API.

    It has already been answered and I now understand but what in the file class API would have help me understand about exception handling?

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    The hint to the File API was for checking if the file exists part.

    The exceptions bit is all there in that tutorial.

Similar Threads

  1. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 02:34 PM
  2. Display Message Without Throwing Exception
    By kailashchandra in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-27-2008, 09:05 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. throwing Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-09-2007, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •