java - Automated IllegalArgumentException message? -


i check arguments of public functions , throw exceptions when something's wrong. (for private helpers use assertions).

like this:

if( < 0 || >= b )     throw new illegalargumentexception("'a' must greater or equal 0 ,          smaller b "); 

but annoys me write these error messages. message seems redundant me, message negation of statement

a < 0 || >= b 

.

it happens rename variable refactoring (in eclipse) , message not reflect changes. or change conditions , forget change messages.

it great, if write like:

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

this should raise illegalargumentexception message

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

in c write macro (actually in c assert macro). there simple way in java?

thank you!

in c language cannot use macros this, in cpp (the c preprocessor) can :-) if want there nothing restricts running cpp on java source before gets compiled. let use cpp style macros (you might need strip lines starting #line cp output.)

to reproduce conditional in exception imho including implementation detail. if exception message describes violation in contract in terms of contract (for instance "no parent object given", "amount cannot negative") don't need change every time condition changes.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -