您的位置:首页 > 编程语言 > Java开发

Java如何定义自己的exception

2009-05-15 12:16 363 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: