#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    Amersfoort, The Netherlands
    Posts
    10
    Rep Power
    0

    IOException not catched


    I have written a method that read a file and interprets the keywords and values in it. It works fine, but if I change the call in the JSP-page and offer the method a filename that doesn't exist, the IOException isn't catched.
    In stead, Apache Tomcat gives the error: Apache Tomcat/4.0.1 - HTTP Status 404 - c:\jdtbc2.txt (The system cannot find the file specified)

    The header of the method in the bean:
    public void read_ini_file(String filename) throws IOException {
    .
    .
    .
    try {
    FileInputStream fis =new FileInputStream(inputfile);
    InputStreamReader isr =new InputStreamReader(fis);
    BufferedReader r =new BufferedReader(isr);
    line=r.readLine();
    //2003-01-08 EHE: (linenumber=in.read())!=-1
    while(line!=null) {
    if(line.length()>0) {
    .
    .
    }
    Catch(FileNotFoundException fnfe) {
    .
    .
    }
    Catch(IOException ioe) {
    .
    .
    }
    Catch(NullPointerException npe) {
    .
    .
    }
    Catch (Exception e) {
    .
    .
    }

    None of the exceptions are ctached when offering the mehod a unknown filename....

    Suggestions?
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    I am not real clear on what you are saying, so I will throw a few things out here.

    The first odd thing I notice is that you specify that your method throws and IOException, yet you are attempting to catch that same IOException in the method. If nothing else this is confusing as to your intentions. Do you wish the caller of the method read_ini_file to catch the IOException or do you want to catch it in the read_ini_file method itself?

    As your code is now, the FileNotFoundException should be caught in the first catch block after your try block (You are getting a FileNotFoundException in this case, which does indeed extend IOException). What are you doing in that catch block since your code fragment is incomplete?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    Amersfoort, The Netherlands
    Posts
    10
    Rep Power
    0
    First of all, thanks for your reply.

    The intention is to catch all exceptions in the method. In the catch section, errorcodes and errormessages are set, which can be read out by getProperties.


    If you have any other suggestions, please let me know.


    Erwin
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14
    You shoul always wrap you reads in a conditional statement to avoid this. I also agree with the previous comment about catching & throwing...its one or the other & usually a good idea to catch the exceptions so you can handle them specifically, not mix them into a bunch of exceptions that you can sort out.

    Here's an example of some code where I read the content for the web page into a servlet...note the conditions:

    java.net.URL url = getServletContext().getResource("/sitePages/"+pageName+".txt");
    java.io.InputStream is = url.openStream();

    if (is != null) {
    java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is));
    String s;
    while ((s = br.readLine()) != null) {
    sb.append(s);
    sb.append("\n");
    }
    br.close();
    is.close();
    }
    DC Dalton
    DCD Designs
    SCJP

IMN logo majestic logo threadwatch logo seochat tools logo