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

R

Roger Redford

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!
 
T

Thomas Schodt

Roger said:
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) {
}


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

brandon

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!



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

---
 
B

Bryce

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

Thomas G. Marshall

Roger Redford said:
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]...
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top