《Java自定义异常举例.ppt》由会员分享,可在线阅读,更多相关《Java自定义异常举例.ppt(17页珍藏版)》请在三一办公上搜索。
1、自定义异常补充例子,南京农业大学谢忠红,题目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
2、=4,删除,添加,Sizefront+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
3、full queue);public FullQueueException(final String s)super(s);class EmptyQueueException extends Exceptionpublic EmptyQueueException()super(temp to delete from a empty queue);public EmptyQueueException(final String s)super(s);,class Queueprotected int item;protected int front=1;protected int back=0;p
4、rotected 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 FullQueueEx
5、ception();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(
6、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.getMe
7、ssage();,自定义异常举例3,题目:在定义银行类时,若取钱数大于余额则作为异常处理。思路:(1)定义自己的异常类 insufficientFundsException(2)取钱(withdrawal)方法中可能产生产生异常,条件是余额少于取额.(3)处理异常安排在调用withdrawal的时候,因此withdrawal方法要声明抛出异常,由上级方法调用。,异常程序:class InsufficientFundsException extends Exception private Bank excepbank;/银行对象 private double excepAmount;/要取的钱
8、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(d
9、ouble 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()(The balance is+(int)balance);,public class ExceptionDemo public
10、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个字符放入一个字符数组,
11、并在屏幕上显示它们的程序。4.请处理数组越界异常。ArrayIndexOutOfBoundsException5.下面的程序有何错误?,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(MyException e)System.out.println(e.toString();static void myMethod()throws MyException throw new MyException();class MyException extends Exception public String toString()return 自定义异常;,