What's wrong with this try/catch/finally?
5 Message(s) by 5 Author(s) originally posted in java developer
| From: dba_222 |
Date: Thursday, August 12, 2004
|
Dear experts,
I'm trying to learn JAVA on my own. I picked up a
sample online, but it isn't 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() + " isn't 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 not 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've looked at, it looks the same as
other try/catch/finally. I've 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 not find the file " + args[i]);
}
}------------------------------------------------But, how to get the original to work with /try/catch/finally? Thanks a lot!
| From: Thomas Schodt |
Date: Thursday, August 12, 2004
|
wrote in
message :
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() isn't 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.
| From: brandon |
Date: Thursday, August 12, 2004
|
wrote in message:
Dear experts,
I'm trying to learn JAVA on my own. I picked up a
sample online, but it isn't 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() + " isn't 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 not 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've looked at, it looks the same as
other try/catch/finally.
I've 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 not 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.");
---
| From: Bryce |
Date: Thursday, August 12, 2004
|
On 12 Aug 2004 06:02:59 -0700, dba_222@xxxxxxxxxxx (Roger Redford)
wrote in message:
Java c -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
| From: Thomas G. Marshall |
Date: Saturday, August 14, 2004
|
Roger Redford
<dba_222@xxxxxxxxxxx> coughed up the following:
Dear experts,
I'm trying to learn JAVA on my own. I picked up a
sample online, but it isn't 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 do not 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/.
Next Message: JAVA design pattern question