简体   繁体   English

如何不在控制台上打印异常

[英]How not to print exception on console

I implemented my own custom exception. 我实现了自己的自定义异常。 I do not want it to print the exception on the framework console. 我不希望它在框架控制台上打印异常。 Is it possible? 可能吗?

May 15, 2017 2: 47: 24 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet[project - services] in context with path[/project-services] threw exception [Request processing failed; 2017年5月15日2:47:24 PM org.apache.catalina.core.StandardWrapperValve为路径为[/ project-services]的servlet [project-services]调用SEVERE:Servlet.service()引发异常[请求处理失败; nested exception is ba.project.exception.TAException: There is no any tour activity between selected dates.] with root cause ba.project.exception.TAException: There is no any tour activity between selected dates.at ba.project.service.TAServices.findByTourTypeWithDates(TAServices.java: 94) 嵌套的异常是ba.project.exception.TAException:在选定的日期之间没有任何巡回活动。]根本原因ba.project.exception.TAException:在ba.project.service的选定的日期之间没有任何巡回活动。 TAServices.findByTourTypeWithDates(TAServices.java:94)

Custom Exception: 自订例外:

 public class TAException extends RuntimeException {

  private static final long serialVersionUID = 1 L;

  public TAException(String msg) {
    super(msg);
  }

  public TAException(String msg, Throwable e) {
    super(msg, e);
  }
}

Here is how I throw exception: 这是我引发异常的方法:

public List < Object > findByTourTypeWithDates(String tourType, Date checkin, Date checkout)
throws ParseException, TAException {
  SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

  Date todayDate = dateFormatter.parse(dateFormatter.format(new Date()));
  if (checkin.after(todayDate)) {
    return taDAO.findByTourTypeWithDates(tourType, checkin, checkout);
  }
  throw new TAException("There is no any tour activity between selected dates.");
}

Take a look to the specification of Java Exception 看一下Java Exception的规范

https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

I think what you are searching for is writableStackTrace, you can set it to false if needed. 我认为您要搜索的是writableStackTrace,可以根据需要将其设置为false。 Or you can override getMessage to check in with package you are or something like this. 或者,您可以重写getMessage,以使用您所使用的软件包或类似内容进行检入。 It depend on your exact needs. 这取决于您的确切需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM