Shortest code to throw IllegalArgumentException in Java

21

Okey, we all know the normal way to throw a IllegalArgumentException in Java:

throw new IllegalArgumentException(); // 37 characters

But there must be a shorter (as in less characters) ways to do so. How can we produce a java.lang.IllegalArgumentException with even less code?

  • The code fragment has to compile and run in java 7.
  • No imports/external packages (e.g. not using java.util.Arrays.toString() )
    • only exception: java.lang because it is automatically imported.
  • You can add own methods/classes.
  • It must throw a java.lang.IllegalArgumentException
    • Edit: the error output (stacktrace) must name it java.lang.IllegalArgumentException, so no subclasses of it.

To have a base to start from:

class Titled {
    public static void main(String[] args) {
        throw new IllegalArgumentException();
    }
}

luckydonald

Posted 2014-09-06T18:38:15.877

Reputation: 419

Answers

29

These were all found by grepping the source code in the package java.lang.

All of them result in a "pure" IllegalArgumentException (i.e. not a subclass of it).

The ones marked * only work if you add " throws Exception" (18 characters) to your main declaration, as they throw a checked exception of some kind.

12 (30?) characters*

"".wait(-1);

This will result in:

java.lang.IllegalArgumentException: timeout value is negative

22 (40?) characters*

new Thread().join(-1);

22 characters

Character.toChars(-1);

30 characters

Character.UnicodeBlock.of(-1);

Doorknob

Posted 2014-09-06T18:38:15.877

Reputation: 68 138

No, it will not compile because it can throw a InterruptedException. – luckydonald – 2014-09-06T21:11:45.313

1The compiler will error: unreported exception InterruptedException; must be caught or declared to be thrown – luckydonald – 2014-09-06T21:12:13.173

@luckydonald Hm, it does? I tested it on Ideone and it seemed to work. – Doorknob – 2014-09-06T21:14:21.590

1Whoops, Ideone automatically adds throws Exception to the main method. I've added a note in my post. – Doorknob – 2014-09-06T21:17:28.373

Ideone tends to automatically add throws java.lang.Exception which will allow the InterruptedException to pass the compiler. (Also Ideone imports java.util.* and java.io.*) – luckydonald – 2014-09-06T21:20:10.850

1Mwhahaha! I managed to find my IllegalArgumentException by memory. No need for tools like grep. Except the changed rules make it invalid. :-( – Justin – 2014-09-06T22:31:13.007

@Quincunx sorry about that. But it is still an nice way. – luckydonald – 2014-09-07T12:35:01.307

Is 1.wait(-1) legal in Java? In C# it is. – usr – 2014-09-07T20:18:43.567

1@usr No; primitives aren't objects in Java. – Doorknob – 2014-09-07T20:37:16.127

You can get to eleven characters by renaming args to a and using a.wait(-1) instead. – JohnEye – 2014-09-08T14:39:48.613

I thought it was illegal to wait on an object unless you owned the object's monitor, via synchronized. (In this case, it could actually matter, too, since the object representing "" is actually the same object representing every other empty string literal.) – AJMansfield – 2014-09-09T12:26:41.393

23

Here's a nice short way to do it, in 17 13 chars:

new Long("");

It throws a NumberFormatException, which is an IllegalArgumentException. This and this verify it.

Equivalently, one could do

new Byte("");

Justin

Posted 2014-09-06T18:38:15.877

Reputation: 19 757

Sorry, clearified what I am looking for: I am looking a for 'clean' IllegalArgumentException. It should name it so. – luckydonald – 2014-09-06T19:35:06.207

@luckydonald what do you mean by "naming"? The type given in the stacktrace? If so, would an exception-with-cause be acceptable if the IllegalArgumentException was passed internally as the cause to another exception? – Reinstate Monica - ζ-- – 2014-09-07T11:52:28.920

The purpose is to replace the normal throw new IAE(). The code, when executed in a static (main) method has to fail Exception in thread "main" java.lang.IllegalArgumentException at Untitled.main(Titled.java:4). Were it fails (line, file, stacktrace) doesn't matter. – luckydonald – 2014-09-07T12:32:51.140

10

22 characters:

Character.toChars(-1);

Running example
Javadoc: java.lang.Character.toChars(int)

Some nice looking variants:

Character.toChars(~4); // 22 characters, number can be any non-negative (and -0)
Character.toChars(1<<7); // 24 characters

~i is the same as -1 * (i+1) because it inverts the bits. So we will get a illegal parameter, because it is smaller then 0.
1<<7 will create a too high number by shifting the 1 seven times. (same as multiply it 7 times with 2). The last accepted Value seems to be 1114111, 1114112 will fail. Note: this might change depending on your environment, and could be not always reliable.

See the Oracle Docs "Bitwise and Bit Shift Operators" and "Primitive Data Types"

28 characters:

And if you don't like using the character class in a character count competition*:

Enum.valueOf(Enum.class,""); // 28 characters

*) Just to make this pun.

luckydonald

Posted 2014-09-06T18:38:15.877

Reputation: 419

Doesn't need to be any positive; you can do any non-negative (ie ~0 works too) – Justin – 2014-09-07T14:33:04.903

I thought about 0 as a positive because it has no minus. But your right, and even Character.toChars(~-0); works. – luckydonald – 2014-09-07T16:45:59.093

4~0 is -1. Not sure what ~-0 does for you, besides requiring an additional character. – alex.forencich – 2014-09-07T19:37:01.683

It looks funnier xD – luckydonald – 2014-09-07T20:58:22.063

8

21 characters:

System.getProperty("");

As per the documentation, getProperty and setProperty throw IllegalArgumentException if the key is empty.

ApproachingDarknessFish

Posted 2014-09-06T18:38:15.877

Reputation: 951

5

25 Characters

Creates a vector with an invalid (negative) length:

new java.util.Vector(-1);

Displays:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal Capacity: -1
    at java.util.Vector.<init>(Vector.java:129)
    at java.util.Vector.<init>(Vector.java:144)
    at Titled.main(Titled.java:3)

David

Posted 2014-09-06T18:38:15.877

Reputation: 51

1

Here's 24 characters:

System.out.printf("%z");

This will throw an IllegalFormatException, which is an IllegalArgumentException.

AJMansfield

Posted 2014-09-06T18:38:15.877

Reputation: 2 758

1

19 characters:

String.format("%");

Throws java.util.UnknownFormatConversionException, which inherits from IllegalFormatException, which, in turn, inherits from IllegalArgumentException;

c.P.u1

Posted 2014-09-06T18:38:15.877

Reputation: 1 049

1

14 Characters

this.wait(-1);

17 Characters

Thread.sleep(-1);

As far as code that directly throws IllegalArgumentException, these will do it.

From documentation: 
Thread.sleep(int millis): 
Throws:IllegalArgumentException - if the value of millis is negative
InterruptedException - if any thread has interrupted the current thread. 

so direct code is 17 chars, if you're being a super stickler and counting the chars to add a throws clause for the interupted exception you can shorten it by just throwing the raw Exception class

jpcoder

Posted 2014-09-06T18:38:15.877

Reputation: 11

1this.wait(-1) does not work in the main function, due to main being static. – luckydonald – 2015-07-14T02:08:57.657

Thread.sleep(-1) requires you to add a throws declaration to the function, so it gets longer again. error: unreported exception InterruptedException; must be caught or declared to be thrown – luckydonald – 2015-07-14T02:13:20.327