What's wrong with this try/catch/finally?

From: Roger Redford (dba_222_at_yahoo.com)
Date: 08/12/04

  • Next message: Ian Vellosa: "Ant and refid's"
    Date: 12 Aug 2004 06:02:59 -0700
    
    

    Dear experts,

    I'm trying to learn java on my own. I picked up a
    sample online, but it is not compiling right:

    ------------------------------------------------

    import java.io.*;

    public class FileInfo {

      public static void main(String[] args) {
      
        for (int i = 0; i < args.length; i++) {
          File f = new File(args[i]);
          if (f.exists()) {
            System.out.println("getName: " + f.getName());
            System.out.println("getPath: " + f.getPath());
            System.out.println("getAbsolutePath: " + f.getAbsolutePath());
            try {
              System.out.println("getCanonicalPath: " + f.getCanonicalPath());
            }
            catch (IOException e) {
            }
            System.out.println("getParent: " + f.getParent());
            if (f.canWrite()) System.out.println(f.getName() + " is writable.");
            if (f.canRead()) System.out.println(f.getName() + " is readable.");
            if (f.isFile()) {
               System.out.println(f.getName() + " is a file.");
            }
            else if (f.isDirectory()) {
              System.out.println(f.getName() + " is a directory.");
            }
            else {
              System.out.println("What is this?");
            }
            if (f.isAbsolute()) {
              System.out.println(f.getName() + " is an absolute path.");
            }
            else {
              System.out.println(f.getName() + " is not an absolute path.");
            }
            try {
              System.out.println("Last Modified" + f.lastModified());
              System.out.println(f.getName() + " is " + f.length() + " bytes.");
              System.out.println(f.getName() + " is " + f.length() + " bytes.");
            }
            catch (IOException e) {
            }
          
          }
          else {
             System.out.println("I'm sorry. I can't find the file " + args[i]);
          }
          
        }
      
      } /* main */

    } /* class */

    ------------------------------------------------

    Javac -classpath %CLASSPATH%;. FileInfo.java
    FileInfo.java:44: exception java.io.IOException is never thrown in body of
    corresponding try statement
            catch (IOException e) {
            ^

    ------------------------------------------------

    And yet, from what I have looked at, it looks the same as
    other try/catch/finally.

    I have fudged it this way, to get it compile:

    ------------------------------------------------

       try {
          System.out.println(args[0]);
            System.out.println("Last Modified: " + f.lastModified());
              System.out.println(f.getName() + " is " + f.length() + " bytes.");
            }

        finally {
          System.out.println("finally");
        }
      

              }
    /*
            try {
              System.out.println("Last Modified" + f.lastModified());
                    System.out.println(f.getName() + " is " + f.length() + " bytes.");
    catch (IOException e) {
            }

            }
    */
          
          }
          else {
             System.out.println("I'm sorry. I can't find the file " + args[i]);
          }
          
        }

    ------------------------------------------------

    But, how to get the original to work with /try/catch/finally?

    Thanks a lot!


  • Next message: Ian Vellosa: "Ant and refid's"