CrazyEngineers
  • Can anyone explain the exception concept in this JAVA program?

    zia.sepsis

    zia.sepsis

    @ziasepsis-0NGPzn
    Updated: Oct 23, 2024
    Views: 878
    import java.io.*;
    public class new
    {
    public static void main(String args[])
    {
    String name;
    BufferedReader reader;
    reader=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("\n What is your name?");
    try
    {
    name=reader.readLine();
    System.out.println("hello,"+name+"!");
    }
    catch (IOException ioe)
    {
    System.out.println("I/o exception error");
    }
    }
    }

    can anyone explain the exception concept in this program?????
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • safwan

    MemberAug 1, 2009

    hi zia .
    { sory for spamming }
    this is for shallini.
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberAug 1, 2009

    In the given code the readLine() method on object of BufferedReader is being called. This functions returns the string for the line read from the specified input stream and it throws an IOException if there is any error in reading data.

    Whenever any method declares the 'throws' clause (i.e., it declares that it can throw a particular exception) then the calling method should:
    - Either implement a try catch block to catch that exception like done in given code where calling method implements a try catch block to catch IOException.
    - Or the method should declare the 'throws' clause in its signature.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • zia.sepsis

    MemberAug 1, 2009

    pradeep_agrawal
    In the given code the readLine() method on object of BufferedReader is being called. This functions returns the string for the line read from the specified input stream and it throws an IOException if there is any error in reading data.

    Whenever any method declares the 'throws' clause (i.e., it declares that it can throw a particular exception) then the calling method should:
    - Either implement a try catch block to catch that exception like done in given code where calling method implements a try catch block to catch IOException.
    - Or the method should declare the 'throws' clause in its signature.

    -Pradeep
    Thanks mate.... so do you mean that... if the user doesn't give any input, the catch block will be executed.. am i right with my view????
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberAug 1, 2009

    zia.sepsis
    so do you mean that... if the user doesn't give any input, the catch block will be executed.. am i right with my view????
    No, the program will wait till user gives any input, it will not throw any exception. It will throw exception if there is some error in reading the input and at that time the catch block will be executed.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register