Import Java-Java Programminng-Lab Lecture, Lecture notes of Java Programming

This lecture was delivered by Prof. Mudita Tiwari at Cochin University of Science and Technology for Java Programming course. It includes: Import, Java, Program, Client, Public, Class, String, Input, Stream, Reader, System

Typology: Lecture notes

2011/2012

Uploaded on 07/19/2012

rohit-sharma
rohit-sharma 🇮🇳

4.3

(11)

200 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
import java.io.*;
// This program catches the exception when the word "client" is
// entered incorrectly.
public class TestException
{
static String s = "";
//--------------------------------------------------------
public static void main (String args[])
{
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(is);
System.out.println("Enter the word you cannot spell: ");
try
{
s = buf.readLine();
}
catch (IOException e)
{
System.out.println("IOException was " + e.getMessage());
}
try
docsity.com
pf3

Partial preview of the text

Download Import Java-Java Programminng-Lab Lecture and more Lecture notes Java Programming in PDF only on Docsity!

import java.io.*; // This program catches the exception when the word "client" is // entered incorrectly. public class TestException { static String s = ""; //-------------------------------------------------------- public static void main (String args[]) { InputStreamReader is = new InputStreamReader(System.in); BufferedReader buf = new BufferedReader(is); System.out.println("Enter the word you cannot spell: "); try { s = buf.readLine(); } catch (IOException e) { System.out.println("IOException was " + e.getMessage()); } try

checkSpelling(); // this method throws SpellException } catch (SpellException se) // but it is caught here { System.out.println("Spell exception was: " + se.getError()); } } // end main //---------------------------------------------------------- // Check spelling of typed in word. Throw exception if wrong. // Note how this method specifies that it throws such and such // exception. Does not have to be caught here. private static void checkSpelling() throws SpellException { if (s.equalsIgnoreCase("client")) System.out.println("OK"); else throw new SpellException("Cannot spell client"); } } // end main class //*********************************************** // Custom exception class that descends from Java's Exception class. class SpellException extends Exception {