zoukankan      html  css  js  c++  java
  • JAVA面向对象(5)

    一、         异常

    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….

                 

    public class Test3 {
        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("欢饮您使用本程序!");
            }
        }
    }
    示例代码

    3、              常见的异常类型?表6-1

    4、              throw和throws2个关键字都是用于抛出异常

    区别有3点,P149页

    1、          作用不同

    2、          位置不同

    3、          内容不同

    二、         log4j:1款开源的日志记录工具

    使用步骤:

    1、          在项目中添加log4j .jar文件

    2、          创建log4j.propterties文件【保存日志信息的相关设置】

    3、         编写日志的配置信息【输出级别、目的地、格式】

    4、          在程序中,使用log4j记录日志(sql日志、异常日志、业务日志等3种类型的日志信息)

  • 相关阅读:
    巴洛克式和哥特式的区别
    推荐阅读书籍,是时候再行动起来了。
    AtCoder ABC 159F Knapsack for All Segments
    AtCoder ABC 159E Dividing Chocolate
    AtCoder ABC 158F Removing Robots
    AtCoder ABC 158E Divisible Substring
    AtCoder ABC 157F Yakiniku Optimization Problem
    AtCoder ABC 157E Simple String Queries
    AtCoder ABC 157D Friend Suggestions
    AtCoder ABC 156F Modularness
  • 原文地址:https://www.cnblogs.com/etid/p/7009722.html
Copyright © 2011-2022 走看看