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("********** 计算结束 ***********") ;
        }
    };
  • 相关阅读:
    函数模板、函数模板特化、重载函数模板、非模板函数重载
    输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)
    文件的读写、二进制文件的读写、文件随机读写
    文件流(fstream, ifstream, ofstream)的打开关闭、流状态
    流类库继承体系(IO流,文件流,串流)和 字符串流的基本操作
    对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
    operator new 和 operator delete 实现一个简单内存泄漏跟踪器
    异常与继承、异常与指针、异常规格说明
    程序错误、异常(语法、抛出、捕获、传播)、栈展开
    C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
  • 原文地址:https://www.cnblogs.com/tszr/p/12435813.html
Copyright © 2011-2022 走看看