Java自定义异常举例

上传人:cl****1 文档编号:470330262 上传时间:2023-06-27 格式:DOC 页数:19 大小:229KB
返回 下载 相关 举报
Java自定义异常举例_第1页
第1页 / 共19页
Java自定义异常举例_第2页
第2页 / 共19页
Java自定义异常举例_第3页
第3页 / 共19页
Java自定义异常举例_第4页
第4页 / 共19页
Java自定义异常举例_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《Java自定义异常举例》由会员分享,可在线阅读,更多相关《Java自定义异常举例(19页珍藏版)》请在金锄头文库上搜索。

1、耳自定义异常补充例子南京农业大学谢忠红2自定义异常举例题目2编写一个循环队列类Queue,属性:队列数组、队列的头指针front、尾指针back、队列长size构造函数方法:从队头删除元素III从队尾插入元素自定义异常 一个队列可能有两个出错的原因: (1)空队列时试图删除一个元素:(2)满队列时试图添 加一个元素。试用EmptyQueueException 和 FullQueueException异常耒分别表示这询种错误。 队列抛出异常对象说明所抛出的异常类型。backfront添加+初始状态:Size=0空队歹!j删除ISize=4Size front+ + If(front=6)Fron

2、t=0backSize+back+If (back=6)back=O自定义异常一个队列可能有两个 出错的原因:(1)空队列时试图 删除一个元素(2)或者满队列时 试图添加一个元素。 Class Exception extends Throwable Exception () super(); Exception (String y) super(s); class FullQueueException extends Exception public FullQueueException () super(ntemp to add a item to a full queue); public

3、 FullQueueException(final String ) super (s); class EmptyQueueException extends Exception! public EmptyQueueException() super(Mtemp to delete from a empty queue11); public EmptyQueueException(final String s)super(s);class Queueprotected int item; protected int front=l;protected int back=O; 1 protect

4、ed int size=O;public Queuef int length ) item=new intlength; public boolean isFull()if (size=itemength) return true;elsereturn false;public boolean isEmpty()if (size=0) return true; elsereturn false;Public void addBack(int unit) th rows FullQueueException if (isFullO) throw new FullQueueExceptionf);

5、size+; back+;if (back=ite mength) back=O;itemback=unit;试写出删除元素的方法public int removeFront()throws EmptyQueueException int unit;if (isEmptyO)throw new EmptyQueueException();size;u n it=item fro nt;front+;if (front =itemength )front=0;return unit ; public class JavaQueueExceppublic static void main(Stri

6、ng args )int a=1a2,3,4a5,6;Queue queuel=new Queue(5);try for (int i=O;i=queuel.itemength;i+) queuel.addBack(ai); catch ( FullQueueException e)System.out.println(e.getMessageO); try for (int i=O;i0.0) balance+=dAmount; | 取钱的throws InsufficientFundsExceptionif (balancevdAmount)throw newIn sufficie ntF

7、undsExcepti on (this, dAmount); balance=balance-dAmount;Apublic void showBalance()System.OUt.printlnCThe balance is lv+(int)balance);public class ExceptionDemo public static void mainfString args)try ba.withdrawal(lOO);ank ba=newank(50);System.out.println(lvWithdrawal successful!vv);catch(Insufficie

8、ntFundsException e) System.out.println(e.toStringO);System.out.println(e.excepMessage();寸.作业1什么是异常?简述兀va的异常处理机制。2系统定义的异常与用户自定义的异常有何 不同?如何使用这两类异常?3. 编写从键盘读入10个字符放入一个字符数 组,并在屏幕上显示它们的程序。4. 请处理数组越界异常。ArraylndexOut0fBoundsExcept i on5 下面的程序有何错误?public class Quizl public static void main(String args ) myM

9、athod();myMathod() throw new MyException();class My Exception public String toString() return 自定义异常改过后的程序蛊public class Quizl public static void main(String args) try myMethod(); catch (MyException e)System.out.println(e.toString(); static void myMethod() throws MyException throw new MyException(); class MyException extends Exception public String toString() return ”自定义异常”;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号