Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 383|回复: 0

java 异常与记录日志

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-5-27 01:04:51 | 显示全部楼层 |阅读模式

      一. 你可能还想利用java.util.logging工具将输出记录到日志中

    package exceptions;
    //: exceptions/LoggingExceptions.java
    // An exception that reports through a Logger.
    import java.util.logging.*;
    import java.io.*;
    
    class LoggingException extends Exception {
      private static Logger logger =
        Logger.getLogger("LoggingException");//Logger.getLogger()方法创建了一个String参数相关联的Logger对象
                          //(通常是与错误相关的包名或类名),这个Logger对象会将其输出发送到System.err
      public LoggingException() {
        StringWriter trace = new StringWriter(); 
        printStackTrace(new PrintWriter(trace));//printStackTrace不会产生默认字符串,为了获取字符串,我们使用重载的
         //printStackTrace()方法,它接受一个java.io.PrintWriter()对象作为参数,如果我们将一个SrtingWriter对象传递给
         //这个Printwrite()构造器,那么就可以调用toString()方法,就可以抽取为一个String
        logger.severe(trace.toString());//severe写入日志
      }
    }
    
    public class LoggingExceptions {
      public static void main(String[] args) {
        try {
          throw new LoggingException();
        } catch(LoggingException e) {
          System.err.println("Caught " + e);
        }
        try {
          throw new LoggingException();
        } catch(LoggingException e) {
          System.err.println("Caught " + e);
        }
      }
    } /* Output: (85% match)
    Aug 30, 2005 4:02:31 PM LoggingException <init>
    SEVERE: LoggingException
            at LoggingExceptions.main(LoggingExceptions.java:19)
    
    Caught LoggingException
    Aug 30, 2005 4:02:31 PM LoggingException <init>
    SEVERE: LoggingException
            at LoggingExceptions.main(LoggingExceptions.java:24)
    
    Caught LoggingException
    *///:~

    二. 尽管LoggingException将所有记录日志的基础设施都构建在了异常自身中,使用它非常方便.但更常见的是我们需要捕获和记录其它人编写的异常,

    package exceptions;
    //: exceptions/LoggingExceptions2.java
    // Logging caught exceptions.
    import java.util.logging.*;
    import java.io.*;
    
    public class LoggingExceptions2 {
      private static Logger logger =
        Logger.getLogger("LoggingExceptions2");
      static void logException(Exception e) {
        StringWriter trace = new StringWriter();
        e.printStackTrace(new PrintWriter(trace));
        logger.severe(trace.toString());
      }
      public static void main(String[] args) {
        try {
          throw new NullPointerException();
        } catch(NullPointerException e) {
          logException(e);
        }
      }
    } /* Output: (90% match)
    Aug 30, 2005 4:07:54 PM LoggingExceptions2 logException
    SEVERE: java.lang.NullPointerException
            at LoggingExceptions2.main(LoggingExceptions2.java:16)
    *///:~

               我们还可以进一步自定义异常,比如加入额外的构造器和成员,因为我们必须在异常处理信息中生成日志信息,新的异常添加了字段x以及设定x值的构造器和读取数据,的方法,此外,还覆盖了getMassge()方法,以产生更详细的信息,对异常来说getMassge()方法有点类似于toString()方法

    package exceptions;
    //: exceptions/ExtraFeatures.java
    // Further embellishment of exception classes.
    import static net.mindview.util.Print.*;
    
    class MyException2 extends Exception {
      private int x;
      public MyException2() {}
      public MyException2(String msg) { super(msg); }
      public MyException2(String msg, int x) {
        super(msg);
        this.x = x;
      }
      public int val() { return x; }
      public String getMessage() { //相当于toString()方法
        return "Detail Message: "+ x + " "+ super.getMessage();
      }
    }
    
    public class ExtraFeatures {
      public static void f() throws MyException2 {
        print("Throwing MyException2 from f()");
        throw new MyException2();
      }
      public static void g() throws MyException2 {
        print("Throwing MyException2 from g()");
        throw new MyException2("Originated in g()");
      }
      public static void h() throws MyException2 {
        print("Throwing MyException2 from h()");
        throw new MyException2("Originated in h()", 47);
      }
      public static void main(String[] args) {
        try {
          f();
        } catch(MyException2 e) {
          e.printStackTrace(System.out);
        }
        try {
          g();
        } catch(MyException2 e) {
          e.printStackTrace(System.out);
        }
        try {
          h();
        } catch(MyException2 e) {
          e.printStackTrace(System.out);
          System.out.println("e.val() = " + e.val());
        }
      }
    } /* Output:
    Throwing MyException2 from f()
    MyException2: Detail Message: 0 null
            at ExtraFeatures.f(ExtraFeatures.java:22)
            at ExtraFeatures.main(ExtraFeatures.java:34)
    Throwing MyException2 from g()
    MyException2: Detail Message: 0 Originated in g()
            at ExtraFeatures.g(ExtraFeatures.java:26)
            at ExtraFeatures.main(ExtraFeatures.java:39)
    Throwing MyException2 from h()
    MyException2: Detail Message: 47 Originated in h()
            at ExtraFeatures.h(ExtraFeatures.java:30)
            at ExtraFeatures.main(ExtraFeatures.java:44)
    e.val() = 47
    *///:~

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-2 21:45 , Processed in 0.073902 second(s), 27 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表