Results 1 to 12 of 12
  1. #1
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default [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

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    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.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    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?

  4. #4
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    btw I'd like a cool java logo if u've already made one :)

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default More about OO design that this specific case

    Quote Originally Posted by sfe23 View Post
    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?
    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:

    Java Code:
    int x = 1;
    switch (x) {
      case 1 :
        System.out.println("This is the only possibility");
        break;
    }
    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:
    public String getStringNoExceptions() {
      try {
        return getString();
      } catch (IOException ioex) {
        throw new InternalError("Damn, this shouldn't happen");
      }
    }
    Now, a caller to getStringNoExceptions() doesn't have to "fuss" with the exception handling.

  6. #6
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Neil, thanks so much for your detailed reply.
    I understand all the fuss about it now :)

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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();

  8. #8
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    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.

  9. #9
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    sfe23: :) Here it is:

    It will automatically resize when you make it your avatar.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by JohnnyR View Post
    An input 'String' from System.in won't throw an error, any thing you enter can be entered into a string.
    System.in can be set to any arbitrary InputStream via System.setIn(InputStream in). If the underlying InputStream throws an exception (or is closed), then System.in will indeed throw an IOException.

  11. #11
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    thanks MK12 :)
    I wonder why it doesn't show it though, I uploaded it to be my profile pic!

  12. #12
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    It has to be your Avatar, not profile pic.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 05:11 AM
  2. GUI IOException
    By serfster in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 05:19 AM
  3. Replies: 3
    Last Post: 04-10-2008, 10:01 AM
  4. java.io.IOException: invalid header field
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 12:09 AM
  5. Variable passing, Error: IOException
    By fernando in forum New To Java
    Replies: 3
    Last Post: 07-31-2007, 03:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •