zoukankan      html  css  js  c++  java
  • 10

    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("欢饮您使用本程序!");
            }
        }
    }
    View Code
    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("运行结束");
            }
    }
    View Code

        异常

    1.                    什么是异常?

    java程序在运行过程中出现的意外情况

    2.                    java中如何进行异常处理?

    java中的异常处理机制

                 try、catch、finally、throw、throws

                                                                try

                                                                                        //有可能出现异常的代码

              }catch(异常类型){

                                 //对出现的异常进行捕获和处理

                          return;

                                                                                        //System.exit(1);// finally语句块将不执行

                          }finally

                       //不管程序是否发生异常,都要执行的代码

                    }

               try…catch…finally一共有3种组合方式

                       try…catch…(catch可以有多种,但要注意先子类后父类的顺序)

                       try…catch…finally

                       try…finally


    3. 程序执行过程中出现的影响程序正常运行的现象
           3.1 异常语法
            try{
                //代码块   
            }catch(异常类型 e){

            }catch(异常类型 e2){

            }...{

            }finally{
           
            }
           注意:try 表示可能出现异常的代码块
             catch 抓取异常,并进行处理
                   可以抓取多个异常,异常的范围要从小到大抓取
                    并且只会执行第一个匹配的异常类型
             finally 最终的 不管是否出现异常,finally中的代码块始终会执行
                 除虚拟机停止(System.exit(1))这种情况外
             注意:finally 和 return 的执行顺序 先执行return
              把返回结果保存在返回结果区域,并没有返回,在执行finally 最后 把保存在结果区域的结        果返回给调用者
           3.2 throws 抛出异常
            a.就是当当前方法,不能解决这个异常的时候,必须把这个异常交给上一个调用者去处理
            b.语法
                访问修饰符 返回值类型 方法名(参数列表)[throws 异常]{
                }

        4.java异常体系
            Throwable
                |--    error:(出现不能通过程序处理的错误)       
                |--    Rxception::(可以同程序抓取或者抛出的错误)
                    |--检查异常(非运行时异常):编译阶段会出现的异常
                       SQlException
                       IOException
                       ClassNotFoundException
                    |--非检查异常(运行时异常RunTimeException):运行简单会出现的异常
                       NullPointerException
                       ArrayIndexOutOfBoundException
                            ClassCastExeption
                注意:checked 异常,是必需处理的
                      运行时异常,可以不处理

  • 相关阅读:
    scrapy 命令行传参 以及发送post请求payload参数
    scrapy框架+selenium的使用
    python 制作GUI页面以及多选框、单选框
    上线操作
    在Linux中使用selenium(环境部署)
    解读Java NIO Buffer
    Maven自定义Archetype
    解决spark streaming集成kafka时只能读topic的其中一个分区数据的问题
    在windows下使用pip安装python包遇到缺失stdint.h文件的错误
    maven-shade-plugin插件未生效原因分析
  • 原文地址:https://www.cnblogs.com/wojiatingting/p/7021410.html
Copyright © 2011-2022 走看看