How to preserve the original stack trace when exception is wrapped in another custom exception and rethrown





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have created Spring boot application with the following layers




  1. Controller

  2. BusinessLayer

  3. DAOImplLayer


Controller calls the Business layer and Business layer calls the DAOImplLayer. I have created two different custom exceptions (BusinessException and DAOException)



In DAOImpl class I'm catching DataAccessException and throwing DAOException (with dataAccessException object in parameter since SonarQube is complaining to Either log or rethrow DataAccessException exception.)



catch (DataAccessException dataAccessException)
{
throw new DAOException(dataAccessException, INTERNAL_SERVER_ERROR, CODE_INTERNAL_SERVER,
dataAccessException.getCause().getMessage());
}


In BusinessImpl I'm catching DAOException and throwing BusinessException



catch (SecurityDAOException e)
{
some logic
throw new BusinessException(e, e.type, e.code, UNABLE_TO_PROCESS_REQUEST);
}


I've written common Handler class to log the exceptions.



  @ExceptionHandler(BusinessException.class)
protected ResponseEntity<Object> handleEntityNotFound(BusinessException businessException)
{
if (null != businessException.getCause())
{
LOG.error("BusinessException: ", businessException);
}
some logic
}




  1. How to preserve the original stack trace. I am looking of something similar



    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.exception.copy.Layer3.getLayer3(Layer3.java:8)
    at com.exception.copy.Layer2.getLayer2(Layer2.java:9)
    at com.exception.copy.Layer1.main(Layer1.java:9)



  2. Is it fine to have a custom exception for each layer?











share|improve this question

























  • Should throw just an exception and getting it in exception handler. Looks like no useful double exceptions wrapped

    – Jonathan Johx
    Jan 4 at 5:08











  • @JonathanJohx: I can't figure out what your comment is trying to say. Care to rewrite it in regular English?

    – ruakh
    Jan 8 at 17:02











  • OK, he should leave the exception is thrown instead of wrap it in a new exception. His block try and catch is not useful because he is throwing it again. Then he should rewrite the exception handler of BusinessException.class to SecurityDAOException.class @ruakh

    – Jonathan Johx
    Jan 8 at 17:18




















0















I have created Spring boot application with the following layers




  1. Controller

  2. BusinessLayer

  3. DAOImplLayer


Controller calls the Business layer and Business layer calls the DAOImplLayer. I have created two different custom exceptions (BusinessException and DAOException)



In DAOImpl class I'm catching DataAccessException and throwing DAOException (with dataAccessException object in parameter since SonarQube is complaining to Either log or rethrow DataAccessException exception.)



catch (DataAccessException dataAccessException)
{
throw new DAOException(dataAccessException, INTERNAL_SERVER_ERROR, CODE_INTERNAL_SERVER,
dataAccessException.getCause().getMessage());
}


In BusinessImpl I'm catching DAOException and throwing BusinessException



catch (SecurityDAOException e)
{
some logic
throw new BusinessException(e, e.type, e.code, UNABLE_TO_PROCESS_REQUEST);
}


I've written common Handler class to log the exceptions.



  @ExceptionHandler(BusinessException.class)
protected ResponseEntity<Object> handleEntityNotFound(BusinessException businessException)
{
if (null != businessException.getCause())
{
LOG.error("BusinessException: ", businessException);
}
some logic
}




  1. How to preserve the original stack trace. I am looking of something similar



    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.exception.copy.Layer3.getLayer3(Layer3.java:8)
    at com.exception.copy.Layer2.getLayer2(Layer2.java:9)
    at com.exception.copy.Layer1.main(Layer1.java:9)



  2. Is it fine to have a custom exception for each layer?











share|improve this question

























  • Should throw just an exception and getting it in exception handler. Looks like no useful double exceptions wrapped

    – Jonathan Johx
    Jan 4 at 5:08











  • @JonathanJohx: I can't figure out what your comment is trying to say. Care to rewrite it in regular English?

    – ruakh
    Jan 8 at 17:02











  • OK, he should leave the exception is thrown instead of wrap it in a new exception. His block try and catch is not useful because he is throwing it again. Then he should rewrite the exception handler of BusinessException.class to SecurityDAOException.class @ruakh

    – Jonathan Johx
    Jan 8 at 17:18
















0












0








0


1






I have created Spring boot application with the following layers




  1. Controller

  2. BusinessLayer

  3. DAOImplLayer


Controller calls the Business layer and Business layer calls the DAOImplLayer. I have created two different custom exceptions (BusinessException and DAOException)



In DAOImpl class I'm catching DataAccessException and throwing DAOException (with dataAccessException object in parameter since SonarQube is complaining to Either log or rethrow DataAccessException exception.)



catch (DataAccessException dataAccessException)
{
throw new DAOException(dataAccessException, INTERNAL_SERVER_ERROR, CODE_INTERNAL_SERVER,
dataAccessException.getCause().getMessage());
}


In BusinessImpl I'm catching DAOException and throwing BusinessException



catch (SecurityDAOException e)
{
some logic
throw new BusinessException(e, e.type, e.code, UNABLE_TO_PROCESS_REQUEST);
}


I've written common Handler class to log the exceptions.



  @ExceptionHandler(BusinessException.class)
protected ResponseEntity<Object> handleEntityNotFound(BusinessException businessException)
{
if (null != businessException.getCause())
{
LOG.error("BusinessException: ", businessException);
}
some logic
}




  1. How to preserve the original stack trace. I am looking of something similar



    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.exception.copy.Layer3.getLayer3(Layer3.java:8)
    at com.exception.copy.Layer2.getLayer2(Layer2.java:9)
    at com.exception.copy.Layer1.main(Layer1.java:9)



  2. Is it fine to have a custom exception for each layer?











share|improve this question
















I have created Spring boot application with the following layers




  1. Controller

  2. BusinessLayer

  3. DAOImplLayer


Controller calls the Business layer and Business layer calls the DAOImplLayer. I have created two different custom exceptions (BusinessException and DAOException)



In DAOImpl class I'm catching DataAccessException and throwing DAOException (with dataAccessException object in parameter since SonarQube is complaining to Either log or rethrow DataAccessException exception.)



catch (DataAccessException dataAccessException)
{
throw new DAOException(dataAccessException, INTERNAL_SERVER_ERROR, CODE_INTERNAL_SERVER,
dataAccessException.getCause().getMessage());
}


In BusinessImpl I'm catching DAOException and throwing BusinessException



catch (SecurityDAOException e)
{
some logic
throw new BusinessException(e, e.type, e.code, UNABLE_TO_PROCESS_REQUEST);
}


I've written common Handler class to log the exceptions.



  @ExceptionHandler(BusinessException.class)
protected ResponseEntity<Object> handleEntityNotFound(BusinessException businessException)
{
if (null != businessException.getCause())
{
LOG.error("BusinessException: ", businessException);
}
some logic
}




  1. How to preserve the original stack trace. I am looking of something similar



    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.exception.copy.Layer3.getLayer3(Layer3.java:8)
    at com.exception.copy.Layer2.getLayer2(Layer2.java:9)
    at com.exception.copy.Layer1.main(Layer1.java:9)



  2. Is it fine to have a custom exception for each layer?








java spring-boot exception exception-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 at 16:59







Thiagarajan Ramanathan

















asked Jan 3 at 21:13









Thiagarajan RamanathanThiagarajan Ramanathan

103112




103112













  • Should throw just an exception and getting it in exception handler. Looks like no useful double exceptions wrapped

    – Jonathan Johx
    Jan 4 at 5:08











  • @JonathanJohx: I can't figure out what your comment is trying to say. Care to rewrite it in regular English?

    – ruakh
    Jan 8 at 17:02











  • OK, he should leave the exception is thrown instead of wrap it in a new exception. His block try and catch is not useful because he is throwing it again. Then he should rewrite the exception handler of BusinessException.class to SecurityDAOException.class @ruakh

    – Jonathan Johx
    Jan 8 at 17:18





















  • Should throw just an exception and getting it in exception handler. Looks like no useful double exceptions wrapped

    – Jonathan Johx
    Jan 4 at 5:08











  • @JonathanJohx: I can't figure out what your comment is trying to say. Care to rewrite it in regular English?

    – ruakh
    Jan 8 at 17:02











  • OK, he should leave the exception is thrown instead of wrap it in a new exception. His block try and catch is not useful because he is throwing it again. Then he should rewrite the exception handler of BusinessException.class to SecurityDAOException.class @ruakh

    – Jonathan Johx
    Jan 8 at 17:18



















Should throw just an exception and getting it in exception handler. Looks like no useful double exceptions wrapped

– Jonathan Johx
Jan 4 at 5:08





Should throw just an exception and getting it in exception handler. Looks like no useful double exceptions wrapped

– Jonathan Johx
Jan 4 at 5:08













@JonathanJohx: I can't figure out what your comment is trying to say. Care to rewrite it in regular English?

– ruakh
Jan 8 at 17:02





@JonathanJohx: I can't figure out what your comment is trying to say. Care to rewrite it in regular English?

– ruakh
Jan 8 at 17:02













OK, he should leave the exception is thrown instead of wrap it in a new exception. His block try and catch is not useful because he is throwing it again. Then he should rewrite the exception handler of BusinessException.class to SecurityDAOException.class @ruakh

– Jonathan Johx
Jan 8 at 17:18







OK, he should leave the exception is thrown instead of wrap it in a new exception. His block try and catch is not useful because he is throwing it again. Then he should rewrite the exception handler of BusinessException.class to SecurityDAOException.class @ruakh

– Jonathan Johx
Jan 8 at 17:18














0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029884%2fhow-to-preserve-the-original-stack-trace-when-exception-is-wrapped-in-another-cu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029884%2fhow-to-preserve-the-original-stack-trace-when-exception-is-wrapped-in-another-cu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas