简体   繁体   中英

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

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

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. Or you can override getMessage to check in with package you are or something like this. It depend on your exact needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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