Java自定义异常举例.ppt_第1页
Java自定义异常举例.ppt_第2页
Java自定义异常举例.ppt_第3页
Java自定义异常举例.ppt_第4页
Java自定义异常举例.ppt_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

自定义异常补充例子,南京农业大学 谢忠红,题目2 编写一个循环队列类Queue,属性:队列数组、队列的头指针front、尾指针back、队列长size 构造函数 方法: 从队头删除元素 从队尾插入元素 自定义异常 一个队列可能有两个出错的原因: (1)空队列时试图删除一个元素:(2)满队列时试图添加一个元素。试用EmptyQueueException 和FullQueueException异常类分别表示这两种错误。队列抛出异常对象说明所抛出的异常类型。,2自定义异常举例,Front=1,删除+,添加+,初始状态:Size=0 空队列,front back,4 5 6 7 8 9 10,front,Size=4,删除,添加,Size front+ If(front=6) Front=0,Size+ back+ If (back=6) back=0,自定义异常一个队列可能有两个出错的原因:(1)空队列时试图删除一个元素(2)或者满队列时试图添加一个元素。,Class Exception extends Throwable Exception () super(); Exception (String s)super(s); class FullQueueException extends Exception public FullQueueException () super(“temp to add a item to a full queue“); public FullQueueException(final String s) super(s); class EmptyQueueException extends Exception public EmptyQueueException() super(“temp to delete from a empty queue“); public EmptyQueueException(final String s) super(s); ,class Queue protected int item; protected int front=1; protected int back=0; protected int size=0; public Queue( int length ) item=new intlength; public boolean isFull() if (size=item.length) return true; else return false; public boolean isEmpty() if (size=0) return true; else return false; ,front,删除,添加,Public void addBack(int unit) throws FullQueueException if (isFull() throw new FullQueueException(); size+; back+; if (back=item.length) back=0; itemback=unit; 试写出删除元素的方法,public int removeFront() throws EmptyQueueException int unit; if (isEmpty() throw new EmptyQueueException(); size-; unit=itemfront; front+; if (front=item.length ) front=0; return unit ; ,public class JavaQueueExcep public static void main(String args ) int a=1,2,3,4,5,6 ; Queue queue1=new Queue(5); try for (int i=0;i=queue1.item.length;i+) queue1.addBack(ai); catch ( FullQueueException e) System.out.println(e.getMessage(); ,try for (int i=0;i=queue1.item.length;i+) int b; b=queue1.removeFront(); catch (EmptyQueueException e) System.out.println(e.getMessage(); ,自定义异常举例3,题目:在定义银行类时,若取钱数大于余额则作为异常处理。 思路: (1)定义自己的异常类 insufficientFundsException (2)取钱(withdrawal)方法中可能产生产生异常,条件是余额少于取额. (3)处理异常安排在调用withdrawal的时候,因此withdrawal方法要声明抛出异常,由上级方法调用。,异常程序: class InsufficientFundsException extends Exception private Bank excepbank; /银行对象 private double excepAmount; /要取的钱 InsufficientFundsException(Bank ba, double dAmount) excepbank=ba; excepAmount=dAmount; public String excepMessage() String str=“The balance is“+excepbank.balance + “n“+“The withdrawal was“+excepAmount; return str; /自定义异常,class Bank double balance;/存款数 Bank(double balance)this.balance=balance; public void deposite(double dAmount) if(dAmount0.0) balance+=dAmount; public void withdrawal(double dAmount) throws InsufficientFundsException if (balancedAmount) throw new InsufficientFundsException(this, dAmount); balance=balance-dAmount; public void showBalance() System.out.println(“The balance is “+(int)balance); ,public class ExceptionDemo public static void main(String args) try Bank ba=new Bank(50); ba.withdrawal(100); System.out.println(“Withdrawal successful!“); catch(InsufficientFundsException e) System.out.println(e.toString(); System.out.println(e.excepMessage(); ,作业,1.什么是异常?简述Java的异常处理机制。 2系统定义的异常与用户自定义的异常有何不同?如何使用这两类异常? 3. 编写从键盘读入10个字符放入一个字符数组,并在屏幕上显示它们的程序。 4.请处理数组越界异常。 ArrayIndexOutOfBoundsException 5.下面的程序有何错误?,public class Quiz1 public static void main(String args) myMathod(); myMathod() throw new MyException(); class MyException public String toString() return 自定义异常; ,改过后的程序: public class Quiz1 public static void main(String args) try myMethod(); catch (MyExceptio

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论