Can anyone explain the exception concept in this JAVA program?

zia.sepsis

zia.sepsis

@ziasepsis-0NGPzn Oct 23, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • safwan

    safwan

    @safwan-NH7W5Y Aug 1, 2009

    hi zia .
    { sory for spamming }
    this is for shallini.
  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Aug 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
  • zia.sepsis

    zia.sepsis

    @ziasepsis-0NGPzn Aug 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????
  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Aug 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