Java如何定义自己的exception
来源:互联网 发布:云计算服务 编辑:程序博客网 时间:2024/05/17 19:15
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; }}
- Java如何定义自己的exception
- java如何定义自己的类
- 如何结合Sping与Java封装自己的Exception?
- 如何定义自己的迭代器
- 如何定义自己的控件
- VC try-catch捕获定义自己的exception
- 如何定义自己的EL表达式
- 如何定义自己的异常类
- 【JTharness4_4_0】【5.如何定义自己的Finder】
- 【JTharness4_4_0】【6.如何定义自己的Interview】
- 如何定义自己的异常类
- 编写自己的Exception
- 如何自己定义RetargetAction
- eclipse java 使用自己定义的包
- 自己定义java注解
- CTreeCtrl如何设置和获取自己定义的数据
- C#如何定义和使用自己的特性
- solr中如何定义自己的解析器插件(QParserPlugin)
- 为MSHFlexGrid添加表格编辑功能
- 最近在做手机上的火车天气的查询 发现数据源是个大问题
- 运行java小程序引起IE崩溃一例
- 为什么vs2005建立的cs类要放在app_code文件夹才能在其他页面用?
- Windows操作系统的13个使用窍门,很适用
- Java如何定义自己的exception
- 关于多线程中的可重入函数问题
- python的_threading_local模块
- MFC编程点滴系列2-MFC的消息机制
- std::map requirements
- 进程间的通信方法与比较
- php自动下载文件
- c++ tools
- 趣味智商题