Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 9th, 2006, 05:09 AM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Java IO error

I am a newie in java IO encountering an odd error, my platform is osx tiger, java 1.5.

and program fragment is:

import java.io.*;
....
public static void main(String[] args) {
   byte[] b = {50, 50, 51, 32};
   System.out.write(b);
   System.out.println();
}

When compile it, get "unreported exception javaio.IOException; must be caught or declared to be thrown
    System.out.write(b) "

If I catch the IOException and rewrite it as:

try {System.out.write(b)}
catch (IOException e) {System.err.println(e) }

The above error is fixed. But if I rewrite "System.out.write(b)" as
"System.out.write(b, 2, 1)" getting a compile-time error again:

"exception java.io.IOException is never thrown in body of corresponding try statement
   catch(IOException e) {System.err.println(e)} "

At this stage, when remove the catch statement, compile-time error is fixed again. Can anyone tell me why? thx.



Reply With Quote
  #2 (permalink)  
Old March 20th, 2007, 11:37 PM
Authorized User
 
Join Date: Mar 2007
Location: Hyderabad, A.P, India.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

we can specify
public static void main(String[] args) throws IOException {
}
this can solve u r problem


K.Mallikarjunarao
Reply With Quote
  #3 (permalink)  
Old March 26th, 2007, 07:25 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

If you see the API documentation for the PrintStream class [System.out object is of PrintStream class], the write(bytes[]) method is taken from its parent class FilterOutputStream class and it throws IOException; thats why you've to catch it if you're calling it. And the other form of write() method write(bytes[], off, len) its overridden in PrintStream class and doesn't throw any Exception it handles them in its implementation thats why you get an exception if try to use try..catch around it saying "exception java.io.IOException is never thrown in body of corresponding try statement" as it doesn't throw any exception you need not use try..catch for it.

hope this is helpful.

Regards,
Rakesh
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to delete file System.IO.Delete error maricar C# 13 March 14th, 2014 06:50 AM
Error message - class java.io.BufferedInputStream simone_s Java Basics 4 May 2nd, 2006 12:08 AM
System.IO.FileNotFoundException cuca_macaii2000 BOOK: ASP.NET Website Programming Problem-Design-Solution 1 February 5th, 2005 10:10 AM
IO.MemoryStream from a string planoie ASP.NET 1.0 and 1.1 Professional 2 November 17th, 2003 05:44 PM
java.io.FilePermission while accessing JSP melida JSP Basics 0 October 9th, 2003 05:32 AM



All times are GMT -4. The time now is 03:55 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.