zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVA异常处理学习笔记:异常的基本概念

    public class ExceptionDemo01{
        public static void main(String args[]){
            System.out.println("********** 计算开始 ***********") ;
            int i = 10 ;        // 定义整型变量
            int j = 0 ;            // 定义整型变量
            int temp = i / j ;    // 此处产生了异常
            System.out.println("两个数字相除的结果:" + temp) ;
            System.out.println("********** 计算结束 ***********") ;
        }
    };
    public class ExceptionDemo02{
        public static void main(String args[]){
            System.out.println("********** 计算开始 ***********") ;
            int i = 10 ;        // 定义整型变量
            int j = 0 ;            // 定义整型变量
            try{
                int temp = i / j ;    // 此处产生了异常
                System.out.println("两个数字相除的结果:" + temp) ;
                System.out.println("----------------------------") ;
            }catch(ArithmeticException e){
                System.out.println("出现异常了:" + e) ;
            }
            System.out.println("********** 计算结束 ***********") ;
        }
    };
    public class ExceptionDemo03{
        public static void main(String args[]){
            System.out.println("********** 计算开始 ***********") ;
            int i = 10 ;        // 定义整型变量
            int j = 0 ;            // 定义整型变量
            try{
                int temp = i / j ;    // 此处产生了异常
                System.out.println("两个数字相除的结果:" + temp) ;
                System.out.println("----------------------------") ;
            }catch(ArithmeticException e){    // 捕获算术异常
                System.out.println("出现异常了:" + e) ;
            }finally{    // 作为异常的统一出口
                System.out.println("不管是否出现异常,都执行此代码") ;
            }
            System.out.println("********** 计算结束 ***********") ;
        }
    };
    public class ExceptionDemo04{
        public static void main(String args[]){
            System.out.println("********** 计算开始 ***********") ;
            int i = 0 ;        // 定义整型变量
            int j = 0 ;            // 定义整型变量
            try{
                String str1 = args[0] ;        // 接收第一个参数
                String str2 = args[1] ;        // 接收第二个参数
                i = Integer.parseInt(str1) ;    // 将第一个参数由字符串变为整型
                j = Integer.parseInt(str2) ;    // 将第二个参数由字符串变为整型
                int temp = i / j ;    // 此处产生了异常
                System.out.println("两个数字相除的结果:" + temp) ;
                System.out.println("----------------------------") ;
            }catch(ArithmeticException e){    // 捕获算术异常
                System.out.println("出现异常了:" + e) ;
            }
            System.out.println("********** 计算结束 ***********") ;
        }
    };
    public class ExceptionDemo05{
        public static void main(String args[]){
            System.out.println("********** 计算开始 ***********") ;
            int i = 0 ;        // 定义整型变量
            int j = 0 ;            // 定义整型变量
            try{
                String str1 = args[0] ;        // 接收第一个参数
                String str2 = args[1] ;        // 接收第二个参数
                i = Integer.parseInt(str1) ;    // 将第一个参数由字符串变为整型
                j = Integer.parseInt(str2) ;    // 将第二个参数由字符串变为整型
                int temp = i / j ;    // 此处产生了异常
                System.out.println("两个数字相除的结果:" + temp) ;
                System.out.println("----------------------------") ;
            }catch(ArithmeticException e){    // 捕获算术异常
                // System.out.println("算术异常:" + e) ;
                e.printStackTrace() ;
            }catch(NumberFormatException e){    // 捕获数字转换异常
                System.out.println("数字转换异常:" + e);
            }catch(ArrayIndexOutOfBoundsException e){    // 捕获数组越界异常
                System.out.println("数组越界异常:" + e) ;
            }
            System.out.println("********** 计算结束 ***********") ;
        }
    };
    public class ExceptionDemo06{
        public static void main(String args[]){
            System.out.println("********** 计算开始 ***********") ;
            int i = 0 ;        // 定义整型变量
            int j = 0 ;            // 定义整型变量
            try{
                String str1 = args[0] ;        // 接收第一个参数
                String str2 = args[1] ;        // 接收第二个参数
                i = Integer.parseInt(str1) ;    // 将第一个参数由字符串变为整型
                j = Integer.parseInt(str2) ;    // 将第二个参数由字符串变为整型
                int temp = i / j ;    // 此处产生了异常
                System.out.println("两个数字相除的结果:" + temp) ;
                System.out.println("----------------------------") ;
            }catch(ArithmeticException e){    // 捕获算术异常
                // System.out.println("算术异常:" + e) ;
                e.printStackTrace() ;
            }catch(NumberFormatException e){    // 捕获数字转换异常
                System.out.println("数字转换异常:" + e);
            }catch(ArrayIndexOutOfBoundsException e){    // 捕获数组越界异常
                System.out.println("数组越界异常:" + e) ;
            }catch(Exception e){
                System.out.println("其他异常:" + e) ;
            }
            System.out.println("********** 计算结束 ***********") ;
        }
    };
    public class ExceptionDemo07{
        public static void main(String args[]){
            System.out.println("********** 计算开始 ***********") ;
            int i = 0 ;        // 定义整型变量
            int j = 0 ;            // 定义整型变量
            try{
                String str1 = args[0] ;        // 接收第一个参数
                String str2 = args[1] ;        // 接收第二个参数
                i = Integer.parseInt(str1) ;    // 将第一个参数由字符串变为整型
                j = Integer.parseInt(str2) ;    // 将第二个参数由字符串变为整型
                int temp = i / j ;    // 此处产生了异常
                System.out.println("两个数字相除的结果:" + temp) ;
                System.out.println("----------------------------") ;
            }catch(Exception e){
                System.out.println("其他异常:" + e) ;
            }catch(ArithmeticException e){    // 捕获算术异常
                // System.out.println("算术异常:" + e) ;
                e.printStackTrace() ;
            }
            System.out.println("********** 计算结束 ***********") ;
        }
    };
    public class ExceptionDemo08{
        public static void main(String args[]){
            System.out.println("********** 计算开始 ***********") ;
            int i = 0 ;        // 定义整型变量
            int j = 0 ;            // 定义整型变量
            try{
                String str1 = args[0] ;        // 接收第一个参数
                String str2 = args[1] ;        // 接收第二个参数
                i = Integer.parseInt(str1) ;    // 将第一个参数由字符串变为整型
                j = Integer.parseInt(str2) ;    // 将第二个参数由字符串变为整型
                int temp = i / j ;    // 此处产生了异常
                System.out.println("两个数字相除的结果:" + temp) ;
                System.out.println("----------------------------") ;
            }catch(Exception e){
                System.out.println("其他异常:" + e) ;
            }
            System.out.println("********** 计算结束 ***********") ;
        }
    };
  • 相关阅读:
    Bootstrap的下拉菜单float问题
    style标签进行实时编辑及修改css(转)
    立即调用的函数表达式IIFE
    标签球Js插件
    如何正确使用Google搜索
    练习一:四则运算
    ARM伪指令
    vim 命令总结
    dialog BLE SDK 学习(1)
    ARM指令集
  • 原文地址:https://www.cnblogs.com/tszr/p/12435813.html
Copyright © 2011-2022 走看看