How does Java define its own exception?

Source: Internet
Author: User

import java.io.*;
// A Java application to demonstrate making your own Exception class// 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{  String mistake;
//----------------------------------------------// Default constructor - initializes instance variable to unknown
  public SpellException()  {    super();             // call superclass constructor    mistake = "unknown";  }  
//-----------------------------------------------// Constructor receives some kind of message that is saved in an instance variable.
  public SpellException(String err)  {    super(err);     // call super class constructor    mistake = err;  // save message  }  
//------------------------------------------------  // public method, callable by exception catcher. It returns the error message.
  public String getError()  {    return mistake;  }}  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.