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 953195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:00:48+00:00 2026-05-16T00:00:48+00:00

I always check the arguments of public functions and throw exceptions when something’s wrong.

  • 0

I always check the arguments of public functions and throw exceptions when something’s wrong. (For private helpers I use assertions).

Like this:

if( a < 0 || a >= b )
    throw new IllegalArgumentException("'a' must be greater or equal to 0 and 
        smaller than b ");

But it always annoys me to write these error messages. The message seems redundant to me, as the message is just the negation of the statement

a < 0 || a >= b

.

It also often happens that I rename the variable with refactoring (in eclipse) and then the message does not reflect the changes. Or I change the conditions and forget to change the messages.

It would be great, if I could write something like:

assertArgument(a >= 0 && a < b);

This should raise an IllegalArgumentException with a message like

"violated argument assertion: a >= 0 && a < b."

In C you could write a macro (actually in C assert is just a macro).
Is there a simple way to do something like this in java?

  • 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-05-16T00:00:48+00:00Added an answer on May 16, 2026 at 12:00 am

    In the C language you cannot use macros to do this, but in cpp (the C preprocessor) you can 🙂 If you really want to do so there is nothing that restricts you from running cpp on your java source before it gets compiled. That would let you use cpp style macros (you might need to strip the lines starting with #line from the cp output.)

    To reproduce the conditional in the exception is IMHO including too much implementation detail. If your exception message describes the violation in the contract in terms of the contract (for instance “no parent object given”, “amount cannot be negative”) you don’t need to change it every time the condition changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always thought that you could use OR in a LIKE statment to query
Its tedious to always check your bank account website for new monthly statements. (They
You always read that for-in loops should check o.hasOwnProperty(k) to skip over Object.prototype. Well,
I'm trying to check network available or not. But in my emulator it always
I always seen on SyncLock examples people using Private Lock1 As New Object '
I always wanted to implement swypable tabs in my application, like the ones in
I always use preg_match and it always works fine, but today I was trying
Most people use pointers like this... if ( p != NULL ) { DoWhateverWithP();
I would like JdbcPersistenceManager to always have just one jdbcConnection and for it to
Is it possible to check what arguments is being passed to a non static

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.