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

Discussion in 'Java' started by Roger Redford, Aug 12, 2004.

  1. 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);
    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);
    }

    }

    } /* 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);
    }

    }


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


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


    Thanks a lot!
     
    Roger Redford, Aug 12, 2004
    #1
    1. Advertisements


  2. To be able to catch a checked exception
    a method inside the try block must declare that it throws that exception
    (or one derived from it).

    File.lastModified() is not declared to throw any exceptions.

    You can completely omit the try/catch block around those three lines
    or you can replace that empty catch block with an empty finally block.

    Note: Empty (and uncommented) catch blocks are commonly frowned upon.
     
    Thomas Schodt, Aug 12, 2004
    #2
    1. Advertisements

  3. Roger Redford

    brandon Guest




    The problem is in this part of the code :

    ---

    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) {
    }

    ---

    In this particular try-block, there are no methods which are capable
    of throwing an IOException, so this IOexception cannot be 'catched'
    in this case.

    You can make this example working by removing the try-catch block,
    like this :

    ---

    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.");

    ---
     
    brandon, Aug 12, 2004
    #3
  4. Roger Redford

    Bryce Guest

    The error tells you what you need to know. Nothing in the surrounding
    try block throws an IOException.
     
    Bryce, Aug 12, 2004
    #4

  5. The others here have given you insight as to the problem, but FWIW I'd like
    to add something.

    When you move up to the jdk1.5 release, there is a -Xlint option that will
    give you every single warning possible for your code. While you certainly
    had enough of an error message from your compiler in this particular case,
    when you move to 1.5 the Xlint warnings will help you understand fundamental
    mistakes based upon assumptions. In theory.

    BTW, comp.lang.java is a "retired" newsgroup, and I don't ever remember
    comp.lang.java.developer ever being valid. Even though you can access the
    unofficial ones, the official ones are:

    comp.lang.java.3d 3D Graphics API's for the Java language.
    comp.lang.java.advocacy Support for and criticism of the Java System.
    comp.lang.java.announce Announcements re the Java System. (Moderated)
    comp.lang.java.beans Java software components (JavaBeans).
    comp.lang.java.corba Topics relating to Java and CORBA.
    comp.lang.java.databases Databases, java.sql, JDBC, ODBC.
    comp.lang.java.gui GUI toolkits and windowing: AWT, IFC etc.
    comp.lang.java.help Set-up problems, catch-all first aid.
    comp.lang.java.machine JVM, native methods, hardware.
    comp.lang.java.programmer Programming in the Java language.
    comp.lang.java.security Security issues raised by Java.
    comp.lang.java.softwaretools IDEs, browsers, compilers, other tools.

    Try to stay within that list.

    ....[stomp]...
     
    Thomas G. Marshall, Aug 14, 2004
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.