1.

package text;

public class text { 
    public static void main(String[] args) { 
        try { 
            	try { 
                	throw new ArrayIndexOutOfBoundsException(); 
            	} 
            	catch(ArrayIndexOutOfBoundsException e) { 
               		System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch"); 
            	}
 
            throw new ArithmeticException(); 
        } 
        catch(ArithmeticException e) { 
            System.out.println("发生ArithmeticException"); 
        } 
        catch(ArrayIndexOutOfBoundsException e) { 
           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
        } 
    } 
}

运行结果:

 

2.

package text;

public class text { 
    public static void main(String[] args) { 
        try {
                try { 
                    throw new ArrayIndexOutOfBoundsException(); 
                } 
                catch(ArithmeticException e) { 
                    System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
                }
            throw new ArithmeticException(); 
        } 
        catch(ArithmeticException e) { 
            System.out.println("发生ArithmeticException"); 
        } 
        catch(ArrayIndexOutOfBoundsException e) { 
            System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
        } 
    } 
}

运行结果:

 

 当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

3.

package text;

public class text {

    
    public static void main(String args[]) {
        
        int result;
        
        try {
            
            System.out.println("in Level 1");

           
             try {
                
                System.out.println("in Level 2");
  // result=100/0;  //Level 2
               
                 try {
                   
                     System.out.println("in Level 3");
                      
                     result=100/0;  //Level 3
                
                } 
                
                catch (Exception e) {
                    
                    System.out.println("Level 3:" + e.getClass().toString());
                
                }
                
                
                finally {
                    
                    System.out.println("In Level 3 finally");
                
                }
                
               
                // result=100/0;  //Level 2

            
                }
            
            catch (Exception e) {
               
                 System.out.println("Level 2:" + e.getClass().toString());
           
             }
             finally {
                
                System.out.println("In Level 2 finally");
           
             }
             
            // result = 100 / 0;  //level 1
        
        } 
        
        catch (Exception e) {
            
            System.out.println("Level 1:" + e.getClass().toString());
        
        }
        
        finally {
           
             System.out.println("In Level 1 finally");
        
        }
    
    }

}

2.运行结果

 

 

 总结:当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

*处理运行异常时,尽量避免,并且尽量用try-catch来辅助。

*在多重catch快后可以加catch(Excetion)来处理遗漏

*对于不确定的也可一加try-catch

*尽量添加finally语句去释放占用的资源,并且finally语句是类似栈来进行释放的。

*还有finally语句不一定执行。

 

二、throw语句

throws语句表明某方法中可能出现某种(或多种)异常,但它自己不能处理这些异常,而需要由调用者来处理。 当一个方法包含throws子句时,需要在调用此方法的代码中使用try/catch/finally进行捕获,或者是重新对其进行声明,否则编译时报错。

throws语句中声明的异常称为受控(checked)的异常,通常直接派生自Exception类。 RuntimeException(其基类为Exception) 和Error(基类为Throwable)称为非受控的异常。这种异常不用在throws语句中声明。 CheckedExceptionDemo.java示例展示了上述两种异常的特性。

 

 

 

 

 

 

 三、以上都是前辈们总结出来的公用的抛出异常,我们在实际应用开发中应该自定义异常与异常处理链。

 

建议:

* 在中间层组件中抛出异常,在界面层组件中捕获异常在底层组件中捕获JVM抛出的“只有程序员能看懂的”异常,转换为中间层的业务逻辑异常,再由界面层捕获以提供有意义的信息。

* 自身能够处理的异常,不要再向外界抛出。

* 尽可能地在靠近异常发生的地方捕获并处理异常

* 尽可能地捕获最具体的异常类型,不要在中间层用 catch(Exception)“吃掉”所有异常

 *在开发阶段捕获并显示所有异常信息,发布阶段要移除部分代码,以避免“过于专业”的异常信息困扰用户,特别地,系统发布之后,不要将服务端异常的详细信息发给客户端,以免被黑客利用

原文地址:http://www.cnblogs.com/zhaoyueheng/p/16817267.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性