Can anyone explain the exception concept in this JAVA program?

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?????

Replies

  • safwan
    safwan
    hi zia .
    { sory for spamming }
    this is for shallini.
  • pradeep_agrawal
    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
  • zia.sepsis
    zia.sepsis
    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????
  • pradeep_agrawal
    pradeep_agrawal
    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

You are reading an archived discussion.

Related Posts

hey me fellow CEans, I am doing enigneering from ip university, Delhi! The college is good and reputed. And i have managed to clear all the backlogs i have as...
Hi 4 all i m from afghanistan, i m hacker is any1 need help can contact me, may many member know by my user AK-47 king of hacker.... i have...
Hi Guys. I am Sudheer,a mechanical engineer and I am in 7 th semister now. I want to write GATE-2010 exam and i had written it in 2009 (got 89...
I have been looking for some Linux stuff out here. Very few people and only computer guys seem to be interested in it. What do you think of having a...
Say some lyrics from a song you know. Let it have some association with the words or idea of the lyrics in the previous post. I have it easy, I...