Results 1 to 12 of 12
Thread: [SOLVED] IOException
- 02-20-2009, 03:05 AM #1
[SOLVED] IOException
I have read the tutorial on Java about exceptions.
I'm looking at a sample code (a simple stack code). The main throws an IOException and the code has a public static String getString() throws IOException as well.
I run the program and no matter what sting I put, I don't get the error so I don't know what the exception does! or when it is called?
Can you tell me when the following method throws the exception?
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
thanks
- 02-20-2009, 03:43 AM #2
That wasn't terribly clear, are you getting an error, or are you wondering why you need to throws it? I'll assume you meant the latter. It doesn't mean necessarily that there is an error, but rather there is a possibility of an error, which must be caught, or acknowledged. Its okay to "throws" exceptions, to pass them on to whatever calls the method, but at some point down the line they must be caught (the possibility of an error being taken care of). Hope this helped.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-20-2009, 03:54 AM #3
MK12,
thanks for your reply.
my question is: what kind of error are we talking about? that method will handle which sort of error?
could you give me an example of this method throwing the exception?
- 02-20-2009, 03:55 AM #4
btw I'd like a cool java logo if u've already made one :)
- 02-20-2009, 04:36 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
More about OO design that this specific case
In practice, it would be massively massively rare in this specific case. OK, if you really want a possible scenario, then just maybe it could happen, say, if the input is piped in from another process, and that process suddenly closes the socket over which it's sending the input.
But... that's kind of besides the point. The reason for having to handle the exception is not about the specifics of System.in; it's about the general case of streams.
The thing that the method is doing ultimately is reading from an InputStream. And streams in general can have I/O errors, so the read methods of InputStream are defined as throwing IOException. This is something that crops up from time to time: you know for 100% that something can't really occur, but the compiler/library design force you to pretend it can. For example, you could be dealing with your own flavour of IOException, which you know never ever throws IOException in practice.
Similarly, if you try and compile this, the compiler will moan that your switch statement is missing a default case, even though you "know" that it would never be reached:
If you find the exception handling messy in such cases, then a solution is to throw some kind of unchecked exception (see my discussion of the Java exception hierarchy). For example:Java Code:int x = 1; switch (x) { case 1 : System.out.println("This is the only possibility"); break; }
Now, a caller to getStringNoExceptions() doesn't have to "fuss" with the exception handling.Java Code:public String getStringNoExceptions() { try { return getString(); } catch (IOException ioex) { throw new InternalError("Damn, this shouldn't happen"); } }Neil Coffey
Javamex - Java tutorials and performance info
- 02-20-2009, 05:02 AM #6
Neil, thanks so much for your detailed reply.
I understand all the fuss about it now :)
- 02-20-2009, 05:12 AM #7
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Heh. It is actually possible for that getString to throw an IOException. To do so, you would need to have called System.setIn(...) on an InputStream that has closed before your 'toString' method is called:
Java Code:FileInputStream in = new FileInputStream("bogus"); System.setIn(in); in.close(); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine();
- 02-20-2009, 05:25 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 46
- Rep Power
- 0
An input 'String' from System.in won't throw an error, any thing you enter can be entered into a string.
The only error would be if u tried to convert it to an int or something?
The way the code set up you wouldn't get an IOError from that.
- 02-20-2009, 01:58 PM #9
- 02-20-2009, 04:43 PM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
- 02-20-2009, 10:35 PM #11
thanks MK12 :)
I wonder why it doesn't show it though, I uploaded it to be my profile pic!
- 02-20-2009, 10:36 PM #12
Similar Threads
-
Error: unreported exception java.io.IOException; ??
By jonsamwell in forum New To JavaReplies: 5Last Post: 08-24-2008, 04:11 AM -
GUI IOException
By serfster in forum New To JavaReplies: 3Last Post: 06-13-2008, 04:19 AM -
java.io.IOException: CreateProcess: matlab error=2
By Jack in forum Advanced JavaReplies: 3Last Post: 04-10-2008, 09:01 AM -
java.io.IOException: invalid header field
By osval in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 11:09 PM -
Variable passing, Error: IOException
By fernando in forum New To JavaReplies: 3Last Post: 07-31-2007, 02:03 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks