Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 17th, 2006, 2:01 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old Apr 17th, 2006, 2:28 PM   #2
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
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.
jaeusm is offline   Reply With Quote
Old Apr 17th, 2006, 5:15 PM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old Apr 17th, 2006, 6:25 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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...
Jimbo is offline   Reply With Quote
Old Apr 17th, 2006, 8:17 PM   #5
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
Quote:
Apparently i need the "finally" key word after the try statement.
Actually, for the code you posted, you don't.
jaeusm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC