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

  • Home
  • SEARCH
  • 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 tend to forget these built-in Symfony functions for making links.
I always use unix timestamps for everything, but am wondering if there is a
I always felt that expecting exceptions to be thrown on a regular basis and
I always seem to use Get when working with data (strongly typed or otherwise)
Most people use pointers like this... if ( p != NULL ) { DoWhateverWithP();
Is it possible to check what arguments is being passed to a non static
It's the first i post to stackoverflow although i always check the forum. I
Why can't we use assertion for public methods? I have read somewhere An assert
NOTE: The restriction is that I cannot use exceptions (the code is eventually compiled
Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort()

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.