

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 3
This page cannot be seen from the preview
Don't miss anything!


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 {