Results 1 to 12 of 12
- 11-05-2009, 09:22 AM #1gcampton Guest
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.
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?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() ... }
- 11-05-2009, 09:32 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Lesson: Exceptions (The Java Tutorials > Essential Classes)
There is no substitute for reading the tutorials.
- 11-05-2009, 09:50 AM #3gcampton Guest
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.
- 11-05-2009, 09:55 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You also need to read the API specs for the File class.
- 11-05-2009, 09:57 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 11-05-2009, 11:21 AM #6gcampton Guest
- 11-05-2009, 11:23 AM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 11-05-2009, 11:24 AM #8gcampton Guest
- 11-05-2009, 11:28 AM #9gcampton Guest
- 11-05-2009, 11:34 AM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
So are you asking if you should read their specs too?
Well it doesn't hurt.
- 11-05-2009, 11:48 AM #11gcampton Guest
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?
- 11-05-2009, 12:44 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Main method throwing specific Exception
By bugger in forum New To JavaReplies: 5Last Post: 05-13-2009, 02:34 PM -
Display Message Without Throwing Exception
By kailashchandra in forum JavaServer Faces (JSF)Replies: 0Last Post: 09-27-2008, 09:05 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
throwing Exception
By bugger in forum New To JavaReplies: 3Last Post: 11-09-2007, 09:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks