gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:57:00 30 4
gpt4 key购买 nike

我实现了自己的自定义异常。我不希望它在框架控制台上打印异常。是否可以?

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; 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)

自定义异常:

 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);
}
}

这是我抛出异常的方法:

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.");
}

最佳答案

看一下Java异常规范

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

我认为您正在搜索的是 writableStackTrace,如果需要,您可以将其设置为 false。或者您可以重写 getMessage 以检查您所在的包或类似的内容。这取决于您的具体需求。

关于java - 如何不在控制台上打印异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43980037/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com