Advertisement

C#中throw抛出异常时如何处理后续代码

阅读量:

1、问题来源参见以下代码:

复制代码
 public void add(int index, E element)

    
 {
    
     if(size >= elements.length) 
    
     {
    
     throw new RuntimeException("顺序表已满,无法添加"); 
    
     //return;  //该句还需要吗?
    
     }
    
     ....
    
 }

2、测试代码及执行情况如下:

复制代码
 //代码1

    
 public static void test() throws Exception  
    
 {
    
  
    
     throw new Exception("参数越界"); 
    
     System.out.println("异常后"); //编译错误,「无法访问的语句」
    
 }
    
  
    
  
    
  
    
  
    
 //代码2
    
 try
    
 {
    
     throw new Exception("参数越界"); 
    
 }
    
 catch(Exception e) 
    
 {

全部评论 (0)

还没有任何评论哟~