5

I define a custom exception like so :

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

In the main program I use this exception like so :

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

Now I don't know how to print stack trace when validatePath fail(means if statement become false) .Can anyone help me to print stack trace in custom exception?

4
  • ValidationException e = new ValidationException(); logger.error("Exception : {}" , e); - what's this meant to do? Commented Sep 27, 2015 at 5:58
  • @ immibis yes.but when I define this statment in ValidationException class give me an error . In readFile() method I want to stop the running if validate Path is false and then print stack tarce in file with logback Commented Sep 27, 2015 at 6:04
  • Why are you logging in the exception constructor?! Maybe you mean logger.error("Error", this)? Commented Sep 27, 2015 at 6:33
  • @George Simms In readFile() method I want to stop the program running by define custom exception and throws in method body then print stack trace with logback but I don't know how to do this? Commented Sep 27, 2015 at 6:41

1 Answer 1

3

Why not just using e.printStackTrace()?

public class ValidationException extends Exception
{
    public ValidationException(String message)
    {
        super(message);
    }
}

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 (ValidationException e) {
        // This will print the stack trace.
        e.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

where do I use try block?In readFile() method?I am new in java .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.