zoukankan      html  css  js  c++  java
  • 异常处理的动手动脑

    一,抛异常的类型

    抛异常:1,Error错误,系统错误 2,Exception错误
    Exception类又包括:RuntimeException非检查异常和检查异常。
    Error例子: 1.AssertionError程序运行期间判断某个条件是否满足
    ,不满足时,抛出AssertionError。

                     2.OOM Error系统内存不足。

    二,小探索

    int i=1, j=0, k;
    k=i/j;


    javac生成idiv字节码指令
    double d1=100,d2=0,result;
        result=d1/d2;
    System.out.println("浮点数除以零:" + data);

    浮点数除以0:Infinity
    javac生成ddiv字节码指令,JVM在具体实现这两个指令时,采用了不同的处理策略,导致两段代码运行时得到不同的结果。

    三,try catch的嵌套

    public class CatchWho { 
        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"); 
            } 
        } 
    }

    ArrayIndexOutOfBoundsException/内层try-catch 发生ArithmeticException

    上述是结果

    public class CatchWho2 { 
        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"); 
            } 
        } 
    }
    ArrayIndexOutOfBoundsException/外层try-catch

    看下面这段代码,

    public class EmbededFinally {
    
        
        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");
            
            }
        
        }
    
    }

    运行结果为

    in Level 1
    in Level 2
    in Level 3
    Level 3:class java.lang.ArithmeticException
    In Level 3 finally
    In Level 2 finally
    In Level 1 finally

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

    四,自定义异常

    Class MyException extends Exception
        { …    }
    Class MyClass {
            void someMethod() {
                if (条件) throw new MyException();
            }
        }


  • 相关阅读:
    delphi算法
    delphi 弹出选择目录窗口
    delphi 导出xml文件
    play 源码分析
    oracle指令
    delphi 环境问题
    如何启动redis
    关于整理和工作小结
    如何强制关闭服务
    delphi之事件
  • 原文地址:https://www.cnblogs.com/xcl666/p/9938913.html
Copyright © 2011-2022 走看看