Results 1 to 3 of 3
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Exception not caught

    Hi,

    I am trying to handle an IOException in a class, but when the exception occurs the interpreter is not printing the exception.

    the code is as below:-

    Java Code:
    import java.io.*;
    class SumHelper
    {
    public String getUserInput(String prompt)
    {
    String userInput = null;
    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
    
    System.out.println(prompt + " ");
    try
    {
    userInput = is.readLine();
    
    }
    catch(IOException e)
    {
    System.out.println("Please enter a valid response");
    }
    return userInput;
    }
    }
    for testing the code above i created a test class as below:-

    Java Code:
    class test
    {
    public static void main(String [] st)
    {
    SumHelper helper = new SumHelper();
    String res = helper.getUserInput("Please enter a valid number");
    
    System.out.println(res);
    }
    }
    Here is the output, I am getting
    Java Code:
    root@developer:/Coding/JAVA/Sum/testing# java test
    Please enter a valid number 
     
    
    root@developer:/Coding/JAVA/Sum/testing#
    I didn't entered anything for the exception to be caught, but the program is behaving in abnormal way.

    Thanks
    Ankit

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Exception not caught

    Hi Ankit,

    Giving an empty input by just pressing the enter key doesn't mean that an IOException will be thrown. Because an empty input is a valid value. The IOException will be thrown if there is a failed or interrupted I/O operation.

    If you want to get an exception try pressing something like CTRL + BREAK key on your keyboard.

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Exception not caught

    Hi wsaryada,

    got your point. :).. thanks

Similar Threads

  1. Replies: 7
    Last Post: 03-21-2012, 08:07 AM
  2. SQLException caught:No data found
    By jttslg in forum NetBeans
    Replies: 0
    Last Post: 04-20-2011, 04:07 PM
  3. SQLException caught: Communications link failure due to underlying exception: **
    By sharanya in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-25-2011, 09:46 AM
  4. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  5. Replies: 2
    Last Post: 10-08-2010, 12:08 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
  •