Using a throws Clause in a Method's Declaration - Java Object Oriented Design

Java examples for Object Oriented Design:Method

Description

Using a throws Clause in a Method's Declaration

Demo Code

import java.io.IOException;

public class Main {
  public static void main(String[] args) {  
    char c = '\u0000';
    try {//from w  w  w  .  j av a  2 s.  c om
      System.out.print("Enter some text and then press Enter key:");
      c = readChar();
      System.out.println("The first character you entered is: " + c);
    } 
    catch(IOException e) {
      System.out.println("Error occurred while reading input.");
                }  
  }
  public static char readChar() throws IOException {
    char c = '\u0000';
    int input = 0;
    input = System.in.read();
    if (input != -1) {
      c = (char)input;
    }
    return c;
  }
}

Result


Related Tutorials