我定义一个自定义的异常,如下所示:
package source.exception;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ValidationException extends Exception
{
private static final Logger logger = LoggerFactory.getLogger("source.exception.ValidationException");
public ValidationException(String message)
{
super(message);
ValidationException e = new ValidationException();
logger.error("Exception : {}" , e);
}
}
在主程序中,我会这样使用这个异常:
public void readFile(String path) throws ValidationException
{
logger.debug("Input file path = {}" , path);
try
{
if(validatePath(path))
{
mathExpressionReader = new BufferedReader(new FileReader(path));
}
else
{
throw new ValidationException("Your file dose not exist!");
}
}
catch(Exception ex)
{
logger.error("Exception {} has occurred" , ex);
}
}
现在我不知道如何在validatePath失败时打印堆栈跟踪(意思是if语句变为false)。有人能帮我在自定义异常中打印堆栈跟踪吗?
ValidationException e = new ValidationException(); logger.error("Exception : {}" , e);
这是什么意思? - user253751logger.error("Error", this)
? - George Simms