zoukankan      html  css  js  c++  java
  • 面向对象10:异常

        异常
    
    1.                    什么是异常?
    
    java程序在运行过程中出现的意外情况
    
    2.                    java中如何进行异常处理?
    
    java中的异常处理机制
    
                 trycatchfinallythrow、throws
    
                                                                try//有可能出现异常的代码
    
              }catch(异常类型){
    
                                 //对出现的异常进行捕获和处理
    
                          return;
    
                                                                                        //System.exit(1);// finally语句块将不执行
    
                          }finally{
    
                       //不管程序是否发生异常,都要执行的代码
    
                    }
    
               trycatch…finally一共有3种组合方式
    
                       trycatch…(catch可以有多种,但要注意先子类后父类的顺序)
    
                       trycatchfinally…
    
                       tryfinally3. 程序执行过程中出现的影响程序正常运行的现象
           3.1 异常语法
            try{
                //代码块    
            }catch(异常类型 e){
    
            }catch(异常类型 e2){
    
            }...{
    
            }finally{
            
            }
           注意:try 表示可能出现异常的代码块
             catch 抓取异常,并进行处理
                   可以抓取多个异常,异常的范围要从小到大抓取
                    并且只会执行第一个匹配的异常类型
             finally 最终的 不管是否出现异常,finally中的代码块始终会执行
                 除虚拟机停止(System.exit(1))这种情况外
             注意:finallyreturn 的执行顺序 先执行return
              把返回结果保存在返回结果区域,并没有返回,在执行finally 最后 把保存在结果区域的结        果返回给调用者
           3.2 throws 抛出异常
            a.就是当当前方法,不能解决这个异常的时候,必须把这个异常交给上一个调用者去处理
            b.语法
                访问修饰符 返回值类型 方法名(参数列表)[throws 异常]{
                }
    
        4.java异常体系
            Throwable
                |--    error:(出现不能通过程序处理的错误)        
                |--    Rxception::(可以同程序抓取或者抛出的错误)
                    |--检查异常(非运行时异常):编译阶段会出现的异常
                       SQlException
                       IOException 
                       ClassNotFoundException
                    |--非检查异常(运行时异常RunTimeException):运行简单会出现的异常
                       NullPointerException
                       ArrayIndexOutOfBoundException
                            ClassCastExeption
                注意:checked 异常,是必需处理的
                      运行时异常,可以不处理
    
    
    public class Test {
        private static Logger logger=Logger.getLogger(Test3.class.getName());
        public static void main(String [] args){
            Scanner input=new Scanner(System.in);
            System.out.print("请输入被除数:");
            int num1=input.nextInt();
            System.out.println("");
            System.out.print("请输入除数:");
            int num2=input.nextInt();
            try{
                System.out.println(num1/num2);
                System.out.println("感谢使用本程序!");
            }catch(InputMismatchException e){
                logger.error("出现错误!除数和被除数必须为整数!",e);
            }catch(ArithmeticException  e){
                logger.error(e.getMessage());
            }catch(Exception e){
                logger.error(e.getMessage());
            }finally {
                System.out.println("欢饮您使用本程序!");
            }
        }
    }
    
    
    public class TestEx {
            public static void main(String[] args){
                Scanner  console = new Scanner(System.in);
                try{
                    System.out.println("try开始");
                    int a = console.nextInt();
                    int b = console.nextInt();
                    System.out.println("try结束");
                }catch(InputMismatchException ee){
                    System.out.println("异常");
                 // ee.printStackTrace();
                    String mes = ee.getMessage();
                    System.out.println(mes);
                }catch(NullPointerException e){
                    System.out.println("输入的不是整数");
                 //    e.printStackTrace();
                }catch(ClassCastException e1){
                    System.out.println("类型转换异常");
                }
                System.out.println("运行结束");
            }
    }
  • 相关阅读:
    单点登录cas常见问题(八)
    11G新特性 -- variable size extents
    11G新特性 -- ASM Fast Mirror Resync
    redhat 6.4 安装VirtualBox自动增强功能功:unable to find the sources of your current Linux kernel
    LINUX使用DVD光盘或者ISO作为本地YUM源
    数据库报ORA-00600: 内部错误代码, 参数: [17059],并产生大量trace日志文件
    Putty设置删除
    ssh/scp 远程连接ssh非默认端口方法
    查看LINUX版本
    RHCE7 -- systemctl命令
  • 原文地址:https://www.cnblogs.com/yangchan250/p/7018155.html
Copyright © 2011-2022 走看看