473,413 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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 11054
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ahmet AKGUN | last post by:
Hi Compiler gives error to code shown below. It says "refering to unassigned object "rdReader" in finally block". Is the code below a bad usage of try-finally ? How can I write this code better...
4
by: Hasani | last post by:
http://www.skidmore.edu/~h_blackw/finally.gif I can only catch the exception thrown in the finally code section. even if I do catch(InvalidOperationException)
8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
24
by: Dave | last post by:
Maybe I'm missing something here, but I can't see the purpose of the 'finally' keyword. What is the difference between: try { doSomething() } catch { handleError(); }
2
by: Mike W | last post by:
I've been looking at some code and started wondering what is the point of 'Finally" in Try Catch Finally. Why not just place the Finally code after the End Try? Will the Catch fire if there is an...
4
by: garyusenet | last post by:
Hi I'm using the following code which is finally working. Public Class Form1 Shared ActElement As Object Shared ActFields As DataSet Public Sub SetActElement() Dim objApp As New Object
16
by: SLIMSHIM | last post by:
Hi, I"m new to c# and .net. I wrote a small program to add rows to an access table. the program goes thru the motions but the data never gets there. here is my code. I am intentionaly not using...
5
by: jobs | last post by:
re: try catch finally to close and dispose, but still want Application_error to fire 1. If catch an exception to to dispose and close of a ado connect, how can I allow the exception to still...
8
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
Is it right when placing the RETURN statement inside the TRY or inside the CATCH statement, when there is a FINALLY clause? Especially when there is a transaction going on, in the try/catch block?...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.