![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
class help
Previous to using a class the following code has no problem
import java.awt.*;
import hsa.Console;
import java.util.Random;
import java.io.*;
public static void main (String [] args) throws IOException
{
BufferedReader input2 = new BufferedReader (new FileReader ("C:\\output.txt"));
intNumberofLines = Integer.valueOf (input2.readLine ()).intValue ();
}But instead of accessing the file this way, constantly through main(). I figured that having a class would do the trick. However, I get this one error that doesn't seem to go away. The error is: error on blue lines "The constructor "FileReader" can throw the checked exception "java.io.FileNotFoundException", but it's invocation is neither enclosed in a try statement that can catch that exception nor in the body of the method or constructor that "throws" that exception."
class Database
{
public int lengthofFile; // holds the file length
public String tempString = ""; // holds the added value from main. This value is to be added to the list
public String oldFile [] = new String [1]; // this will hold the old file
public String newFile [] = new String [1]; // this will hold the updated file
public BufferedReader input2;
// the contructor. When an object is created this is the default method used.
public Database ()
{
// before we can modify the file we need to know how many lines it is, at the moment.
input2 = new BufferedReader (new FileReader ("C:\\output.txt"));
lengthofFile = Integer.valueOf (input2.readLine ()).intValue ();
// try
//{
// c.print ("I'm in Try");
//}
//catch (IOException input2)
// {
// c.println ("Input error");
// /* sets the length of the file
// sothatwecanmodify it later on*/
//oldFile = new String [lengthofFile];
// newFile = new String [lengthofFile + 1];
}
}
}In main() (from code #1). I just put this "(String [] args) throws IOException " on the same line as the main declaration. So how would I avoid this same error in the class if there's no where to put this statement (in the class of the code example #2) . If anyone can help me I'd be so thankful. Any help would be greatly appreciated.
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
You need to catch the Exception that could possibly be thrown. Put those lines in question inside the try block rather than before the try block.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Apparently i need the "finally" key word after the try statement. So how does this work basically? Correct me if I'm wrong: It goes through the try statement. If unsuccessful, what happens? and if successful, it executes everything in the try and up until the "finally" key word?
__________________
Death smiles at us all. All a man can do is smile back. Last edited by Eric the Red; Apr 17th, 2006 at 5:30 PM. |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Your try block executes, and if it throws an exception, you jump down to the catch block for the corresponding exception type. Code in the finally block "always" gets executed, even if an exception is caught. It's a useful place for cleanup code which should be executed regardless of errors. I would assume that a finally block could be bypassed by an unchecked exception... not sure on that though...
|
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|