A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

好久没发帖,话说在函数内部跑出异常并进行catch处理后

try
{
。。。。
代码A;
throw new RuntimeException()
}
catch(RuntimeException e)
{
.........
代码B;
}
代码C;
---------------------------------------------------------
catch里的代码运行完后,是不是后面的代码C都要运行啊?
  1. /*
  2. 堆栈:先进后出
  3. 队列:先进先出

  4. 练习:用Linked模拟一个堆栈或者队列
  5.          并提供方法:存、取、判断空
  6. */
  7. import java.util.*;
  8. class MyQueue //队列 先进先出
  9. {
  10.         private LinkedList link ;
  11.         MyQueue(){
  12.                 link = new LinkedList();
  13.         }
  14.         public void add(Object obj){
  15.                 link.push(obj);//后进的元素角标越小
  16. //                link.addFirst(obj);
  17. //                link.offerFirst(obj);
  18.         }
  19.         public Object get(){
  20.                 return link.pollLast();       
  21.         }
  22.         public boolean isNull(){
  23.                 return link.isEmpty();
  24.         }
  25.         public void printQueue(){
  26.                 System.out.println("队列中的数据:");
  27.                 for(ListIterator it = link.listIterator();it.hasNext();){
  28.                         System.out.println(it.next());
  29.                 }

  30.         }
  31. }
  32. class MyStack
  33. {
  34.         private LinkedList link ;
  35.         MyStack(){
  36.                 link = new LinkedList();
  37.         }
  38.         public void add(Object obj){
  39.                 link.push(obj);//后进的元素角标越小
  40. //                link.addFirst(obj);
  41. //                link.offerFirst(obj);
  42.         }
  43.         public Object get() //throws RuntimeException
  44.         {
  45.                 return link.pollFirst();
  46. /*
  47. try
  48.                 {
  49.                         if(this.isNull())
  50.                                 throw new RuntimeException();
  51. //                        return link.removeFirst();//栈空时报错
  52.                 }
  53.                 catch (RuntimeException e)
  54.                 {
  55.                         System.out.println("异常:栈已空!!No data to pop!!");
  56. //                        e.printStackTrace();
  57.                 }       

  58.                 return link.pollFirst();
  59. */
  60.         }
  61.         public boolean isNull(){
  62.                 return link.isEmpty();
  63.         }
  64.         public void printQueue(){
  65.                 System.out.println("堆栈中的数据:");
  66.                 for(ListIterator it = link.listIterator();it.hasNext();){
  67.                         System.out.println(it.next());
  68.                 }

  69.         }
  70. }
  71. class  LinkedListTest
  72. {
  73.         public static void main(String[] args)
  74.         {
  75.                 MyQueue myq = new MyQueue();
  76.                 myq.add("java00");
  77.                 myq.add("java01");
  78.                 myq.add("java02");
  79.                 myq.add("java03");

  80.                 myq.printQueue();

  81.                 sop("分割线*******队列取数据");
  82.                 sop(myq.get());
  83.                 sop(myq.get());
  84.                 sop(myq.get());
  85.                 sop(myq.get());

  86.                 System.out.println("队列为空:"+myq.isNull());

  87.                 MyStack mys = new MyStack();
  88.                 mys.add("java00");
  89.                 mys.add("java01");
  90.                 mys.add("java02");
  91.                 mys.add("java03");

  92.                 mys.printQueue();
  93.                 sop("分割线*******堆栈取数据");
  94.                 sop(mys.get());
  95.                 sop(mys.get());
  96.                 sop(mys.get());
  97.                 sop(mys.get());
  98.                 sop(mys.get());

  99.                 System.out.println("堆栈为空:"+mys.isNull());
  100.         }

  101.         public static void sop(Object obj){
  102.                 System.out.println(obj);
  103.         }
  104. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马