469,287 Members | 2,635 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

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

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!
Jul 17 '05 #1
3 10846
On 12 Aug 2004 06:02:59 -0700, db*****@yahoo.com (Roger Redford) wrote:
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!

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

---
Jul 17 '05 #2
On 12 Aug 2004 06:02:59 -0700, db*****@yahoo.com (Roger Redford)
wrote:

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


The error tells you what you need to know. Nothing in the surrounding
try block throws an IOException.

--
now with more cowbell
Jul 17 '05 #3
Roger Redford <db*****@yahoo.com> coughed up the following:
Dear experts,

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

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]...

--
http://www.allexperts.com is a nifty way to get an answer to just about
/anything/.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ahmet AKGUN | last post: by
24 posts views Thread by Dave | last post: by
2 posts views Thread by Mike W | last post: by
16 posts views Thread by SLIMSHIM | last post: by
8 posts views Thread by =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.