Docsity

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

Toggle sidebar
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 {