Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2006, 05:09 AM
Registered User
 
Join Date: Mar 2006
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.



 
Old March 20th, 2007, 11:37 PM
Authorized User
 
Join Date: Mar 2007
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
 
Old March 26th, 2007, 07:25 AM
Friend of Wrox
 
Join Date: Mar 2007
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





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





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