Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7909059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:14:14+00:00 2026-06-03T12:14:14+00:00

Ultimately, i’d like to if (badThingsHappen) { log the issue throw exception with description

  • 0

Ultimately, i’d like to

if (badThingsHappen) {
 log the issue
 throw exception with description
}

The obvious redundancy here is that often exception description and the message to be logged is (often) the same.

This looks needlessly verbose

if (badThingsHappen) {
 logger.error("oh no! not again!");
 throw new AppException("oh no! not again!");
}

Declaring temporary String feels wrong

if (badThingsHappen) {
 String m = "oh no! not again!";
 logger.error(m);
 throw new AppException(m);
}

Is it ok to have Exception’s constructor handle the logging? Is there a better (cleaner) way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:14:21+00:00Added an answer on June 3, 2026 at 12:14 pm

    You could use a utility method:

    public class AppException extends Exception {
        public static AppException logAndThrow(Logger logger, String message) throws AppException {
            AppException e = new AppException(message);
            // log the stack trace as well
            logger.error(message, e);
            throw e;
        }
    }
    

    and the use it:

    if (badThingsHappen) {
        AppException.logAndThrow(logger, "oh no! not again!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays that I would like to compare and ultimately wind up
Ultimately, I would like to use Inflector.parameterize to create slugs for article heading that
* Update: Ultimately I've decided that accomplishing exactly what I want here isn't possible
I'm starting a software project that I'd ultimately like to release as free, open-source
I have a procedure that ultimately formats an email message and sends the info
Background Information: Ultimately, I would like to write an emulator of a real machine
I am attempting to generate an Expression tree that ultimately calls a series of
I'm working on a ruby script that ultimately starts up a system process that
I am working on a C++ library. Ultimately, I would like to make it
Ultimately, my goal is to extend Django's ModelAdmin to provide field-level permissions—that is, given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.